Socket.io和Node.Js多个服务器

时间:2021-12-06 23:57:31

I'm new to Web Sockets in general, but get the main concept.

我一般都是Web套接字的新手,但得到了主要概念。

I am trying to build a simple multiplayer game and would like to have a server selection where I can run sockets on multiple IPs and it will connect the client through that, to mitigate connections in order to improve performance, this is hypothetical in the case of there being thousands of players at once, but would like some insight into how this would work and if there are any resources I can use to integrate this before hand, in order to prevent extra work at a later date. Is this at all possible, as I understand it Node.Js runs on a server and uses the Socket.io dependencies to create sockets within that, so I can't think of a possible solution to route it through another server unless I had multiple sites running it separately.

我正在尝试构建一个简单的多人游戏,并希望有一个服务器选择,我可以在多个IP上运行套接字,它将通过它连接客户端,以缓解连接,以提高性能,这是假设的情况下,一次有成千上万的玩家,但是想要了解一下这是如何工作的,如果有任何资源我可以用来预先整合这个,以防止以后的额外工作。这是可能的,因为据我所知,Node.Js在服务器上运行并使用Socket.io依赖项在其中创建套接字,所以我想不出可能的解决方案将其路由到另一台服务器,除非我有多个网站单独运行它。

1 个解决方案

#1


2  

The first question I have is this:

我的第一个问题是:

Are you hosting on AWS or in a local datacenter?

您是在AWS上还是在本地数据中心托管?

The reason I ask is because SOCKET.io requires sticky sessions to work properly across multiple servers. Due to the fact that SOCKET.io will attempt to upgrade each connection, and because that upgrade request must reach the original server that authorized the session, you'll need to route websocket (TCP) connections back to that original server via sticky sessions. Unfortunately AWS makes this extremely tricky and will require you to learn how to:

我问的原因是因为SOCKET.io要求粘性会话在多个服务器上正常工作。由于SOCKET.io将尝试升级每个连接,并且由于该升级请求必须到达授权会话的原始服务器,因此您需要通过粘性会话将websocket(TCP)连接路由回原始服务器。不幸的是,AWS使这非常棘手,需要您学习如何:

A) Modify elastic load balancer policies to forward protocol information B) Split apart TCP connections from standard web requests using something like HA PROXY or NGINX. This is necessary in order to handle web socket UPGRADE requests properly, as you will be setting TCP to sticky and web requests to round-robin. C) Attach your socket.io configuration to a common storage source, like Redis (elasticache).

A)修改弹性负载均衡器策略以转发协议信息B)使用HA PROXY或NGINX等分离来自标准Web请求的TCP连接。这是正确处理Web套接字UPGRADE请求所必需的,因为您将TCP设置为粘性和Web请求为循环。 C)将socket.io配置附加到公共存储源,如Redis(elasticache)。

Once you've figured out what's needed for AWS (or if you've got full control over request routing at your local datacenter), you'll want to architect your SOCKET application to use multicast rooms rather than direct socket messaging.

一旦您弄清楚AWS需要什么(或者如果您完全控制本地数据中心的请求路由),您就需要构建SOCKET应用程序以使用多播室而不是直接套接字消息传递。

Example: To send a message to users in game #4444, emit a message to room 'games:4444', rather than direct to the user's socket.

示例:要向游戏#4444中的用户发送消息,请向房间'游戏:4444'发送消息,而不是直接发送到用户的套接字。

If your socket instance is configured using REDIS, REDIS will automatically take care of maintaining lists of people who are connected to your 'games:4444' channel. Otherwise you'll need to maintain the list yourself using a database or other shared mechanism.

如果使用REDIS配置套接字实例,REDIS将自动负责维护与“游戏:4444”频道相关联的人员列表。否则,您需要使用数据库或其他共享机制自行维护列表。

Other than that, there are plenty of resources online that can help you figure out each step along the way. I'd start with understanding something like HA PROXY and how it can help split apart your SOCKETS from your web requests.

除此之外,网上有大量资源可以帮助您弄清楚每一步。我首先要了解像HA PROXY这样的东西,以及它如何帮助您将SOCKETS与Web请求分开。

#1


2  

The first question I have is this:

我的第一个问题是:

Are you hosting on AWS or in a local datacenter?

您是在AWS上还是在本地数据中心托管?

The reason I ask is because SOCKET.io requires sticky sessions to work properly across multiple servers. Due to the fact that SOCKET.io will attempt to upgrade each connection, and because that upgrade request must reach the original server that authorized the session, you'll need to route websocket (TCP) connections back to that original server via sticky sessions. Unfortunately AWS makes this extremely tricky and will require you to learn how to:

我问的原因是因为SOCKET.io要求粘性会话在多个服务器上正常工作。由于SOCKET.io将尝试升级每个连接,并且由于该升级请求必须到达授权会话的原始服务器,因此您需要通过粘性会话将websocket(TCP)连接路由回原始服务器。不幸的是,AWS使这非常棘手,需要您学习如何:

A) Modify elastic load balancer policies to forward protocol information B) Split apart TCP connections from standard web requests using something like HA PROXY or NGINX. This is necessary in order to handle web socket UPGRADE requests properly, as you will be setting TCP to sticky and web requests to round-robin. C) Attach your socket.io configuration to a common storage source, like Redis (elasticache).

A)修改弹性负载均衡器策略以转发协议信息B)使用HA PROXY或NGINX等分离来自标准Web请求的TCP连接。这是正确处理Web套接字UPGRADE请求所必需的,因为您将TCP设置为粘性和Web请求为循环。 C)将socket.io配置附加到公共存储源,如Redis(elasticache)。

Once you've figured out what's needed for AWS (or if you've got full control over request routing at your local datacenter), you'll want to architect your SOCKET application to use multicast rooms rather than direct socket messaging.

一旦您弄清楚AWS需要什么(或者如果您完全控制本地数据中心的请求路由),您就需要构建SOCKET应用程序以使用多播室而不是直接套接字消息传递。

Example: To send a message to users in game #4444, emit a message to room 'games:4444', rather than direct to the user's socket.

示例:要向游戏#4444中的用户发送消息,请向房间'游戏:4444'发送消息,而不是直接发送到用户的套接字。

If your socket instance is configured using REDIS, REDIS will automatically take care of maintaining lists of people who are connected to your 'games:4444' channel. Otherwise you'll need to maintain the list yourself using a database or other shared mechanism.

如果使用REDIS配置套接字实例,REDIS将自动负责维护与“游戏:4444”频道相关联的人员列表。否则,您需要使用数据库或其他共享机制自行维护列表。

Other than that, there are plenty of resources online that can help you figure out each step along the way. I'd start with understanding something like HA PROXY and how it can help split apart your SOCKETS from your web requests.

除此之外,网上有大量资源可以帮助您弄清楚每一步。我首先要了解像HA PROXY这样的东西,以及它如何帮助您将SOCKETS与Web请求分开。