Fleck WebSockets

I want to use Fleck for my WebSocket project. The server side looks pretty simple, but how do I distinguish between open connections. is there some kind of identifier? The only way I can think of is to create a GUID in the OnOpen event and pass it to the client. is there a more reasonable solution?

The core server is configured:

socket.OnOpen = () => { Console.WriteLine("Open!"); allSockets.Add(socket); }; socket.OnClose = () => { Console.WriteLine("Close!"); allSockets.Remove(socket); }; socket.OnMessage = message => { Console.WriteLine(message); allSockets.ToList().ForEach(s => s.Send("Echo: " + message)); }; 

eg. how would I make a chat room so that all messages receive a message, with the exception of one send.

Fleck server here: https://github.com/statianzo/Fleck

+4
source share
4 answers

Fleck now creates a Guid identifier in WebSocketConnectionInfo for each connected client. This will help in cases where multiple connections use the same IP address. See the corresponding commit here:

https://github.com/statianzo/Fleck/commit/fc037f49362bb41a2bc753a5ff51cc9da40ad824

+6
source

Ask the user to enter the username and attach the username to the socket address:

 var wsimpl = window.WebSocket || window.mozWebSocket; window.ws = new wsimpl('http://localhost:8080/myApp_' + userName, myProtocol); 

then cross out the service-side username after the socket has been opened with socket.WebSocketConnectionInfo.path. There is also a clientip property that can also be used. I do this and it works great.

+2
source

I started something similar to what you ask, I do. In my case, I use ConnectionInfo.Path to distinguish between what my sockets do.

You can get a lot of information already from ConnectionInfo

socket.ConnectionInfo.{Host|Path|Origin|SubProtocol|ClientIPAddress|Cookies}

So, to answer your question, to pass it on to everyone except the sender, you can distinguish each socket based on ConnectionInfo (if applicable, you can also create a UID from this information)


As a very simple example:

If you know that each Client will have a different IP address, something like the following:

  socket.OnMessage = message => { foreach (IWebSocketConnection socketConnection in allSockets.Where(socketConnection => socket.ConnectionInfo.ClientIpAddress != socketConnection.ConnectionInfo.ClientIpAddress)) { socketConnection.Send("Echo: " + message); } }; 
+1
source

This is a different instance of the socket for each client, so I would think that you should do this:

 allSockets.Where(x => x != socket).ToList().ForEach(s => s.Send("Echo: " + message)); 
0
source

Source: https://habr.com/ru/post/1394870/


All Articles