开发者

What is the best practice for WebSocket server(s)?

Lets say you have a site where users may open chat rooms. Once a room is opened, the users may invite others and chat.

Lets also pretend you want to use WebSockets for the communication with the clients. In most server-side WebSocket implementations there is a simple function like connection.broadcast(message) which sends messages to all connected hosts, but not the one who initiated the broadcast. This is a great feature, because you don't need to keep a list of all clients for your self, to keep them updated. The problem with the chat room example is that, in this case, You don't want to broadcast every message t开发者_运维知识库o everyone, but only to the other clients who are in the same room.

This leaves me with two options:

  • Roll my own broadcastToRoom(room_id) function, which in turn means to track all clients and rooms.
  • Spawn a new instance of the server (on a new port) every time a new room gets created.

What would you do?


Just roll your own version of the broadcast for each room you keep a list of users in that room, and if someone sends a message in/to that room just send it to all the users in the room, depending on your protocol you can then exclude the original sender of the message.

You have a list of users for each room anyway, so you can just add the connection as a property to the user object.

But keep in mind that due to recently "discovered" security problem with the current WebSocket specification, both Opera and Mozilla have disabled support for WebSocket until a new version has been issued. So it will take some more time for WebSockets to have widespread availability and server needs to change to with the new version.

For details see:
http://hacks.mozilla.org/2010/12/websockets-disabled-in-firefox-4/
http://blog.pusherapp.com/2010/12/9/it-s-not-websockets-it-s-your-broken-proxy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜