Closed JDClementIII closed 7 years ago
Why not have a Dictionary<Guid, YourStateType>
keyed by WebSocketConnection.ConnectionInfo.Id? Then you wouldn't have to cast every time you accessed the state.
Thanks for the quick reply. I really like and appreciate this library.
I'm currently using the Dictionary approach - as you suggested. Only problem is Dictionary lookups become more expensive as the size of the Dictionary/connections grow. Add to that that this is lookup is an operation that must take place every time a message from a connection is received.
A cast is a low cost operation and remains the same no matter how many connections I have.
This is really more a suggestion and I understand your reasoning. My only thought is that if the Fleck library is already keeping state for me in the form of a Guid...why not make it an object so I can choose the Type of the state that the library is keeping?
Thanks again.
Understandable about the lookup cost. The purpose of the socket is similar to that of .NET's built in Socket, a representation of a connection, not a state bag. After having added it, the guid probably wasn't necessary, but users rely on it now. Another option to skip the lookup is creating the state in scope of WebSocketServer.Start()
.
var server = new WebSocketServer("ws://0.0.0.0:8181");
server.Start(socket =>
{
var state = new YourStateType();
socket.OnMessage = message => HandleIncomingMessage(message, socket, state)
});
Yep. That solves my problem. Thanks!
It would be incredibly helpful to be able to attach a custom State object to the IWebSocketConnection class.
public interface IWebSocketConnection { [...] object State { get; set; } }