多个socket.io服务器共享一个HTTP/S服务器 [英] Multiple socket.io servers sharing a single HTTP/S server

查看:71
本文介绍了多个socket.io服务器共享一个HTTP/S服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ws,Node.js WebSocket库,可以

With ws, Node.js WebSocket library, it is possible to have multiple servers sharing a single HTTP/S server.

是否可以对socket.io做同样的事情?

Is it possible to do the same with socket.io?

我需要在同一HTTP服务器上具有两台WebSocket服务器,一台用于socket.io,另一台用于Apollo订阅.我可以使用Websocket服务器设置Apollo订阅服务器,但不能为socket.io设置socket.io,socket.io仅接受HTTP服务器.

I need to have two WebSocket servers on the same HTTP server, one for socket.io and another one for Apollo subscriptions. I can set up Apollo subscription server with a Websocket server but not for socket.io, socket.io only accepts HTTP server.

我曾想过要做这样的事情:

I thought in doing something like this:

    const socketioWsS = new WebSocket.Server({ noServer: true });
    const graphqlWsS = new WebSocket.Server({ noServer: true });

    const io = socketIo(socketioWsS, {
      transports: ["websocket"]
    });

    server.on("upgrade", function upgrade(request, socket, head) {
      const pathname = url.parse(request.url).pathname;

      if (pathname === "/socket.io/") {
        socketioWsS.handleUpgrade(request, socket, head, function done(ws) {
          socketioWsS.emit("connection", ws, request);
        });
      } else if (pathname === "/graphql") {
        graphqlWsS.handleUpgrade(request, socket, head, function done(ws) {
          graphqlWsS.emit("connection", ws, request);
        });
      } else {
        socket.destroy();
      }
    });

    server.listen(config.app.port, () => {
      ...

      new SubscriptionServer(
        {
          execute,
          subscribe,
          schema
        },
        {
          server: graphqlWsS
        }
      );
    });

它对于Graphql订阅非常有效,但对socket.io则不起作用.

It works well for Graphql subscriptions, but it does not work for socket.io.

推荐答案

您可以将两个socket.io服务器连接到同一Web服务器.为了使其正常工作,每个socket.io实例都必须位于不同的路径上(一个可以是默认路径,一个可以是自定义路径).这意味着您需要在socket.io客户端和socket.io服务器中都设置 path 选项,以匹配其中一台服务器.

You can have two socket.io servers attached to the same web server. To make it work, each socket.io instance needs to be on a different path (one can be the default path and one can be custom). That means you need to set the path option in both socket.io client and socket.io server to match for one of the servers.

请记住,socket.io的客户端和服务器中的默认路径(如果未指定任何内容)是/socket.io ,这就是为什么它响应的原因/socket.io/socket.io.js 为客户端提供socket.io客户端代码.因此,如果您同时更改了两者的路径,则必须调整客户端获取其socket.io代码的方式.

Keep in mind that the default path in both client and server (if you don't specify anything) for socket.io is /socket.io and that is why it responds to /socket.io/socket.io.js to give the client the socket.io client code. So, if you change the path on both, you will have to adjust how the client gets its socket.io code.

以下是在服务器端设置路径的文档: https://socket.io/docs/server-api/,这是客户端文档:

Here's the doc for setting the path on the server-side: https://socket.io/docs/server-api/ and here's the client-side doc: https://socket.io/docs/client-api/#With-custom-path.

如果您确实只想分离传入的socket.io连接,但实际上不必具有两个单独的socket.io服务器实例,则可以使用socket.io命名空间来实现.每个客户端都将连接到一个不同的名称空间(其工作方式类似于路径,但实际上不是路径),然后您可以在服务器端为每个名称空间上的传入连接使用单独的侦听器.正是在这种情况下,socket.io才在webSocket之上添加了此功能.

If you really just want to separate out the incoming socket.io connections, but don't really have to have two separate socket.io server instances, you can do that using socket.io namespaces. Each client would connect to a different namespace (which works like a path, but it isn't really a path) and then you can have separate listeners on the server-side for incoming connections on each namespace. This is a feature that socket.io adds on top of webSocket precisely for circumstances like this.

这篇关于多个socket.io服务器共享一个HTTP/S服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆