Websocket Server Architecture
Need some advice on architecture. I've built a chess site, and want to add multiplayer capabilities. I recentl开发者_开发技巧y 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?
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.
精彩评论