radu-matei / websocket-manager

Real-Time library for ASP .NET Core
https://radu-matei.com/blog/real-time-aspnet-core/
MIT License
452 stars 183 forks source link

Socket still live after client disconnect #52

Closed FabioMorcillo closed 6 years ago

FabioMorcillo commented 6 years ago

Hi,

When client lost connection because crash or modem shutdown, server continue with socket in memory and OnDisconnected event in websockethandler is never executed.

In statup have configuration to keepaliveinterval = 60 seconds, but after this time, socket continue in memory and OnDisconnected event is never executed.

Anyhere have solution to detect when socket client crash to remove socket from memory or make other actions ?

Thank you,

jcyuan commented 6 years ago

I have the same question. how to do heartbeat?

jcyuan commented 6 years ago

problem seems solved: create your own Hub: class myhub : WebSocketHandler { private List<User> _users {get;set;} } then User class: class User { timer.Elapsed += e => { if(elapsed > timeout) onTimeout.emit(); else if(elapsed % interval == 0) onHeartBeat.emit(); } } and in myhub class override onConnected method, you have to create an User and listen to both timeout/heartbeat events once new connection arrives. and once you got timeout event emitted from User class, close the connection and do clear. and do InvokeClientMethodAsync to send heartbeat event for once you got heartbeat event from the user class.

the client should listen to "heartbeat" event and inovke back (send { methodName: "ReceiveHeartBeat" }) to server again, and in myhub class write "ReceiveHeartBeat" method, the WebSocketHnadler (the base class) will find the corresponding method and invoke it for you, in that method, reset user.elapsed = 0.

awosztyl commented 6 years ago

Hi J.C. Have you read this issue https://github.com/radu-matei/websocket-manager/pull/19 ?

There is exception-handling branch

https://github.com/radu-matei/websocket-manager/tree/exception-handling?files=1

18.03.2018 8:56 AM "J.C" notifications@github.com napisał(a):

problem seems solved: create your own Hub: class myhub : WebSocketHandler { private List _users {get;set;} } then User class: class User { timer.Elapsed += e => { if(elapsed > timeout) onTimeout.emit(); else if(elapsed % interval) onHeartBeat.emit(); } } and in myhub class override onConnected method, you have to create an User and listen to both timeout/heartbeat events once new connection arrives. and once you got timeout event emitted from User class, close the connection and do clear. and do InvokeClientMethodAsync to send heartbeat event for once you got heartbeat event from the user class.

the client should listen to "heartbeat" event and inovke back (send { methodName: "ReceiveHeartBeat" }) to server again, and in myhub class write "ReceiveHeartBeat" method, the WebSocketHnadler (the base class) will find the corresponding method to invoke for you, in that method, reset user.elapsed = 0.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/radu-matei/websocket-manager/issues/52#issuecomment-373979497, or mute the thread https://github.com/notifications/unsubscribe-auth/ANQh8b8F8iTUyeU2oWZhXYpzqQBrQUEAks5tfhMQgaJpZM4R-YN3 .

jcyuan commented 6 years ago

@awosztyl thank you, I have just read it. but I wonder why only catch the exception for "All" method eg. sendmessagetoALLasync this error will only occur when the server wants to deal with the client. so what if the server does not send any thing to the client for a long time?

by the way I have a question, what is the correct way to pass the current logged-in user to the WebSocketHandler?

Henry00IS commented 6 years ago

I have fixed the OnDisconnected event. It will always fire now, immediately.

llamas77 commented 6 years ago

Thanks @Henry00IS . Its working!