简体   繁体   English

Socket.io和Webscocket在同一台服务器上侦听

[英]Socket.io and Webscocket listen on the same server

I need to share same http server between socket.io and websocket (from 'ws' package) handlers. 我需要在socket.io和websocket(来自“ ws”包)处理程序之间共享相同的http服务器。 Unfortunatelly, despite that they are listening to diffrent prefixes, the first is listening to /socket.io and the second to /websocket urls, for some reasons if they are running on the same server the websocket is not working properly. 不幸的是,尽管它们正在侦听不同的前缀,但是第一个侦听/socket.io,第二个侦听/ websocket url,由于某些原因,如果它们在同一服务器上运行,则websocket无法正常工作。

I did some debugging, but it seems that the requests are properly handled by both libraries but in the end only socket.io works properly. 我进行了一些调试,但似乎两个库都正确处理了请求,但最终只有socket.io可以正常工作。

Any idea how to solve that? 知道如何解决吗?

The way sockets work in node.js is quite a bit different from the way normal requests work. 套接字在node.js中的工作方式与普通请求的工作方式有很大不同。 There is no routing, so rather than listening to a url, you have to listen to all sockets. 没有路由,因此您必须侦听所有套接字,而不是侦听url。 The default behavior of socket.io is to close any socket connections that it doesn't recognize. socket.io的默认行为是关闭它无法识别的所有套接字连接。 To fix this, you'll need to add the flag 'destroy upgrade': false to the options ( server is an express server): 要解决此问题,您需要在选项中添加'destroy upgrade': false标志'destroy upgrade': falseserver是快速服务器):

require('socket.io').listen(server, {'destroy upgrade': false, ...})

You'll also need to check the url when a client connects (in the code handling /websocket ) and ignore it if it looks like it belongs to socket.io . 您还需要在客户端连接时检查url(在代码处理/websocket ),如果它看起来似乎属于socket.io ,则将其忽略。 You can find the url from the client object (passed in to the on connection handler) as client.upgradeReq.url . 您可以从客户端对象(传入到on connection处理程序中)找到url作为client.upgradeReq.url

Ok solution is simple (unfortunately half day of debugging and now it's simple :)). 好的解决方案很简单(不幸的是调试了半天,现在很简单:))。

There is an option 'destroy upgrade' for upgrade requests coming from non-socketio clients. 对于来自非socketio客户端的升级请求,有一个“销毁升级”选项。 Since Websocket (module 'ws') is using the same requests some of them might be for 'ws' not for 'socket.io'. 由于Websocket(模块“ ws”)正在使用相同的请求,因此其中一些请求可能是针对“ ws”的,而不是针对“ socket.io”的。 So this option should be disabled. 因此,应禁用此选项。

io = require('socket.io').listen(server,  { log: false, 'resource':'/socket.io'  });
io.disable('destroy upgrade') 

Update for 2016: 2016年更新:

io.disable('destroy upgrade');

seems not to be available anymore. 似乎不再可用。 But I succeeded by assigning the websocket module ws a path (using Express): 但是我通过为websocket模块ws分配了一条路径来成功(使用Express):

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

Of course the client has the same path ws://theserver.com/ws 当然,客户端具有相同的路径ws://theserver.com/ws

I did not have to alter the socket.io side at all. 我根本不需要更改socket.io端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM