Replies: 4 comments
-
Resolved this by keeping track of the connections in a separate list and not using _WSServer.ListClients() |
Beta Was this translation helpful? Give feedback.
-
Ok so after digging into this a bit more it seems the issue is these log entries are actually coming from the SendAsync catch exception and probably a queue of messages that still need to be sent. I'm not sure how exactly CancellationTokens work as I have not used them before but would using CancellationTokens help clear up these messages/queue faster? In the SendAsync() function, it seems CancellationTokens are handled in the same way as an exception. As for the CancellationToken implementation and managing the tokens, this is what I have come up with far. However, I'm still getting a disposed exception and not a token cancellation exception. These is the code I have:
Sorry for the noob questions ... still new to C# and just learning. |
Beta Was this translation helpful? Give feedback.
-
I'm sorry I just saw that you re-opened this. Dictionary is not thread-safe. The collection can change if clients connect/disconnect while you're in the middle of iterating during WSSendUpdate. I'd recommend, in WSSendUpdate, to make a copy of the dictionary, and iterate over the copy, where each .SendAsync is encapsulated in a try/catch. |
Beta Was this translation helpful? Give feedback.
-
Either that, or use a Here is a code sample from the earlier recommendation:
And with a lock
The upside with the lock approach is that you're always working on the master copy. The downside is that sending updates locks the dictionary, so it can't be modified while sending updates. Meaning if someone connects or disconnects, the event will hang until the lock is released. |
Beta Was this translation helpful? Give feedback.
-
Hello!
I'm not sure how to do this properly so I could be doing this wrong but basically I'm trying to send a messages to all connected clients. This is the code I'm using:
This works well except for when a client disconnects. Client connects, data streams great no problem, then on disconnect this is what happens:
And this repeats infinitely every time the WSSendUpdate() method is called and bascially locks up the application.
Any help or suggestions on this would be greatly appreciated!
Thanks
Beta Was this translation helpful? Give feedback.
All reactions