3
votes

Here's a slice of the code I can run locally just fine:

Server:

var app = require('express')();

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', 'http://localhost:*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    res.setHeader('Access-Control-Allow-Credentials', true);

    next();
});


app.get('/', function (req, res) {
    console.log("Got '/'");
    res.status(200).send('Hello, world!');
});

var server = app.listen(process.env.PORT || '80', function () {
    console.log('App listening on port %s', server.address().port);
});

var io = require('socket.io')(server);

io.on('connection', function (socket) {
    console.log("connection");
});

Client: (TypeScript, run from a separate app on a different port, http://localhost:8080)

this.socket = io("http://localhost");       
this.socket.on("connect", () => this.onSocketConnect());

When the server si deployed on Google App Engine, and the client is run locally with the address changed to http://my-app-id.appspot.com I get the following error:

WebSocket connection to 'ws://my-app-id.appspot.com/socket.io/?EIO=3&transport=websocket&sid=-Qr3YEeyZLk9K3zmAAAC' failed: Error during WebSocket handshake: Unexpected response code: 400

What's causing this and how can I fix it? Also, once I get this running, I plan to change to https, anything I need to be careful of there?

1

1 Answers

1
votes

At this time, connections to App Engine applications over the ws or wss protocols is not supported. It is possible to get the IP of instance serving the initial requests and then connect to that instance directly but this circumvents App Engine's load balancer which sort of defeats the purpose of deploying your application to App Engine in the first place.

There is currently an open issue (Issue 2535: Web Sockets API) on the App Engine public issue tracker. Feel free to star this issue to support it and receive updates.