的第一个问题我是这样的:
Are you hosting on AWS or in a local datacenter?
我想问的原因是因为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).
一旦你想通了什么需要的AWS(或者,如果你有过请求路由完全控制你的本地数据中心),您需要构建您的SOCKET应用程序以使用多播室而不是直接套接字消息。
例子: To send a message to users in game #4444, emit a message to room 'games:4444', rather than direct to the user's socket.
如果您的插座实例使用Redis的配置,Redis的将自动完成维护谁连接到你的人名单“游戏:4444”通道。否则,您需要使用数据库或其他共享机制来维护自己的列表。
除此之外,网上有大量的资源可以帮助您找出每一步。我首先了解一下诸如HA PROXY之类的东西,以及它如何帮助将您的SOCKETS从Web请求中分离出来。
看看[this](https://github.com/elad/node-cluster-socket.io)写下来。这是一个利用节点“集群”功能水平扩展的实现。这不是你所要求的,而是朝着这个方向迈出的一步。 – dvlsg