javascript - Sharing a WebSocket connection between Socket.IO and WS - Stack Overflow

In an effort to be able to send binary data while utilizing Socket.IO's RPC functionality, I thoug

In an effort to be able to send binary data while utilizing Socket.IO's RPC functionality, I thought that I could use both Socket.IO and the WS module on the same server. Rather than opening up pletely separate servers to make both connections, I am wondering if I can utilize the same HTTP server.

Is it possible to use only one server created with http.createServer() for both Socket.IO and WS at the same time? To be clear, I anticipate creating both a Socket.IO connection and a regular WebSocket connection from the client. The following code creates protocol errors on the client side, presumably because both Socket.IO and WS are attempting to handle the connection.

var http = require('http');
var server = http.createServer(app);
server.listen(3000);

// Socket.IO
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
    // ...
}

// ws
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({server: server});
wss.on('connection', function (ws) {
    // ...
}

In an effort to be able to send binary data while utilizing Socket.IO's RPC functionality, I thought that I could use both Socket.IO and the WS module on the same server. Rather than opening up pletely separate servers to make both connections, I am wondering if I can utilize the same HTTP server.

Is it possible to use only one server created with http.createServer() for both Socket.IO and WS at the same time? To be clear, I anticipate creating both a Socket.IO connection and a regular WebSocket connection from the client. The following code creates protocol errors on the client side, presumably because both Socket.IO and WS are attempting to handle the connection.

var http = require('http');
var server = http.createServer(app);
server.listen(3000);

// Socket.IO
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
    // ...
}

// ws
var WebSocketServer = require('ws').Server
var wss = new WebSocketServer({server: server});
wss.on('connection', function (ws) {
    // ...
}
Share Improve this question asked Nov 3, 2013 at 2:42 BradBrad 164k56 gold badges377 silver badges553 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

It turns out that this is possible with some configuration. The trick is to tell Socket.IO not to destroy non-Socket.IO WebSocket connection requests, and then to put Socket.IO and WS on separate paths. Here is some messy example code, but it works while reusing the Socket.IO session ID for the secondary connection.

var server = http.createServer(app);
server.listen(3000);

var WebSocketServer = require('ws').Server
var io = require('socket.io').listen(server);

io.set('destroy upgrade', false);
io.set('transports', ['websocket']);

io.sockets.on('connection', function (socket) {
    var wss = new WebSocketServer({
        server: server,
        path: '/anythingYouWant/' + socket.id
    });
    wss.on('connection', function(ws) {
        ws.on('message', function(message) {
            console.log(message);
        });
    });
});

As of 2016 I simply could assign the websocket module ws a path

var wss = new WebSocketServer({ server: server, path: '/ws' }); //do not interfere with socket.io

No need to alter the socket.io side at all

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744085206a4556041.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信