Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fleck WebSockets

Tags:

c#

.net

websocket

I want to use Fleck for my WebSocket project, Server side looks pretty straightforward, but how to I differentiate opened connections. is there some sort of ID? The only way I can think of is to create GUID in OnOpen event and pass it back to client. is there a smarter solution?

Basic server set up:

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));
};

E.G. how would I make a chat room so all connection receive message except for the one sending.

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

like image 352
Sergej Popov Avatar asked Jan 18 '23 13:01

Sergej Popov


2 Answers

Fleck now creates a Guid Id on the WebSocketConnectionInfo for every connected client. This will help in cases where multiple connections are using the same IP. See the related commit here:

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

like image 132
statenjason Avatar answered Jan 20 '23 04:01

statenjason


Ask the user for a user name, and attach the username to the the socket address:

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

then strip out the userName on the service side after the socket has been opened with socket.WebSocketConnectionInfo.path. There is also a clientip property that can be used also. I am doing this and it works great.

like image 29
Ben Felda Avatar answered Jan 20 '23 03:01

Ben Felda