admin管理员组文章数量:1395892
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
2 Answers
Reset to default 5It 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
本文标签: javascriptSharing a WebSocket connection between SocketIO and WSStack Overflow
版权声明:本文标题:javascript - Sharing a WebSocket connection between Socket.IO and WS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744085205a2588387.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论