четверг, 1 августа 2019 г.

[Nodejs] Express + Websocket: Connection closed before receiving a handshake response

In case this error you should pass Express listen() return result to WebSocketServer() like this:

wsServer = new WebSocketServer({
    httpServer: app.listen(3000),
    port: 3001,
});

Full example:
var WebSocketServer = require('websocket').server;
var path = require('path');
var express = require('express');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res){
    res.render('index.html');
});
console.log(`Server running on port ${process.env.PORT}`);

wsServer = new WebSocketServer({
    httpServer: app.listen(3000),
    port: 3001,
});

Комментариев нет:

Отправить комментарий