Web sockets management
Hello! I don't understand how to hold a connection with client and how to detect when it was closed by client and remove it from list. I have route where users can subscribe to notification
/notifications/sub
. There I accept web socket and add it to dictionary, id is assigned to each web socket. Then I do the "while" loop because otherwise the connection closes immediately. Also I have a problem that if I close browsers is not removed from dictionary that I use to send messages? Is there a way to solve these problems?
14 Replies
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
I made infinite loop temporary because otherwise the connection with client closes immediately. This is my
WebSocketManagerService
. I store tasks in dictionary, but I don't know what to do next and how can I await for them. I also tried to await for the Task inside Subscribe method, you can see the commented part, but I couldn't connect from client
WebSocketManagerService: https://pastebin.com/98d5UzETPastebin
public class WebSocketManagerService : IWebSocketManagerService{ ...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
ok I think now it shows that client is connected with tcs, but how can I detect when user is disconnected without loop?
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
yes, I have this part
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
yes, maybe I had problem with React that it couldn't connect to ws after a replaced loop with await tsc. I'm right now trying to see the ws ids I have in my dictionary to see if they actually removed after client disconnects (or if it even detects it)
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
I thought it will be easier to use web sockets because SignalR has hubs and etc. Is it better to use SignalR?
I have never used web sockets, signalR or sse, so I decided to start with something that doesn't look complex
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
I added code to remove web socket from dictionary if the state is not open, but I still can see them in my dictionary? I checked that ws should close automatically if there isn't connection to client. or does it throw an exception and should I put code it try catch?
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
yes it's late, thank you for help, I'll try to fix it tomorrow 😀
I've tried signalR and decided to use it for my project, it looks easy to manage than websockets. Also if it's possible can I ask you a question about react?