Websocket服务器架构
需要一些关于架构的建议。我建立了一个国际象棋网站,并想添加多人游戏功能。我最近在这里问了一个关于 url 末尾的路径的作用的问题,如下所示:
"ws://<%= Request.Url.Host %>:<%= WebSocketPort %>/sample"
我现在明白了。因此,如果我需要仅从服务器向所有登录用户发送消息的功能,那么我需要一些东西来处理用户之间的游戏,然后可能需要一个用于聊天的东西,我会有类似以下的东西吗?
var ss = new WebSocket('ws://<%= Request.Url.Host %>:<%= SecureWebSocketPort %>/server');
var gs = new WebSocket('ws://<%= Request.Url.Host %>:<%= SecureWebSocketPort %>/games');
var cs = new WebSocket('ws://<%= Request.Url.Host %>:<%= SecureWebSocketPort %>/chat');
然后在这三个方面,我可以相应地设置我的事件。这似乎可行,但是,这似乎不是正确的方法。我想我发回的消息可能包含有关如何分离事物的信息,但我认为它会超载。
关于如何分离上述功能有什么想法吗?
Need some advice on architecture. I've built a chess site, and want to add multiplayer capabilities. I recently asked a question here on what does the path at the end of the url do as in:
"ws://<%= Request.Url.Host %>:<%= WebSocketPort %>/sample"
I understand it now. So, if I needed the capability to only send messages from the server to all logged in users, then I need something to handles games between users, and then one for chatting maybe, would I have something like the following?
var ss = new WebSocket('ws://<%= Request.Url.Host %>:<%= SecureWebSocketPort %>/server');
var gs = new WebSocket('ws://<%= Request.Url.Host %>:<%= SecureWebSocketPort %>/games');
var cs = new WebSocket('ws://<%= Request.Url.Host %>:<%= SecureWebSocketPort %>/chat');
Then on these three, I could set up my events accordingly. This seems like it would work, however, it doesn't seem like the right way to go. I suppose the message I send back could have info on how to separate things but it would be overloaded I think.
Any thoughts on how I might want to separate the functionality as described above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的是同一整体应用程序的不同通道和消息类型。我认为更合适(且更有效)的解决方案是建立到单个 URL 的单个 WebSocket 连接,然后定义您自己的消息和会话管理。
如果 Socket.IO 适合您,那么有很多此类事情的示例和秘诀。在 google 上搜索“Socket.IO 聊天”、“Socket.IO 会话”、“Socket.IO 授权”等。Socket.IO wiki 可能是一个非常好的起点。
即使您不使用 Socket.IO,您仍然可能需要使用自己的消息和会话管理的单个连接。对于这类事情,使用 JSON 消息可能是可行的方法(因为国际象棋/聊天的带宽不会成为问题)。大多数语言都可以非常轻松地对本机数据类型进行 JSON 序列化/反序列化。
What you are describing is really different channels and message types for the same overall application. I think a more appropriate (and efficient) solution would be to establish a single WebSocket connection to a single URL and then define your own messages and session management.
If Socket.IO is an option for you, there are lots of examples and recipes for this type of thing. Search on google for "Socket.IO chat", "Socket.IO session", "Socket.IO authorization", etc. The Socket.IO wiki is probably a really good place to start.
Even if you don't use Socket.IO, you still probably want a single connection with your own messages and session management. For this sort of thing, using JSON messages is probably the way to go (since bandwidth for chess/chat is not going to be an issue). Most languages have really easy JSON serialization/deserialization to native data types.