I am creating a websocket chat application and I managed to relay chat messages to other browsers connected. I have a console application listening on one port.
My question is… If one person logs on to the system I want everybody to know that, how can I do that? I’m using Linq to map the DB but if the logging is ok how do I send that message, that user X has logged in?
FINALLY I was able to create a chatroom using websockets, here is the final product, thanks for the orientation!
First make sure you’re sending messages as JSON (JavaScript Object Notation) as this allows structured data to be sent back and forth, and client & server can differentiate between a chat message and an instruction (e.g. someone new logged in). For instance on the client:
On the server in ASP.NET C# you’d send them as:
As Hightechrider says, you’ll need to keep track of a list of connected clients, that’s what
WebSocketCollectionclass does in the code listing above.Check out Paul Batum’s WebSocket chat example on github here (https://github.com/paulbatum/BUILD-2011-WebSocket-Chat-Samples/blob/master/BasicAspNetChat/ChatHandler.cs)
Also he did a presentation at the recent MS BUILD conference here (http://channel9.msdn.com/Events/BUILD/BUILD2011/SAC-807T)