oatpp / oatpp-websocket

oatpp-websocket submodule.
https://oatpp.io/
Apache License 2.0
78 stars 32 forks source link

Question: Recommendations on Reconnecting? #26

Closed xanderdunn closed 3 years ago

xanderdunn commented 3 years ago

Do you have any recommendations on the architecting of a WebSocket client connection that reconnects on error or disconnection? I believe there are several situations where this would be desirable:

I attempted to put this in my client coroutine's handleError:

return yieldTo(&ClientCoroutine::act); // re-establish connection

but it did not rescue my AsyncWebSocket from an Invalid communication state error. What is the right way to close out a connection and restart it? Is there additional state that needs to be wiped clean to get it out of a bad frame or other error state?

One solution is to entirely replace the AsyncWebsocket with a new one, but I haven't found how to close and destruct the old one.

lganzzzo commented 3 years ago

Hello @xanderdunn ,

You have to repeat the whole connect->handshake->create_websocket thing.

but it did not rescue my AsyncWebSocket from an Invalid communication state error. What is the right way to close out a connection and restart it? Is there additional state that needs to be wiped clean to get it out of a bad frame or other error state?

Since AsyncWebSocket is associated with a connection - you can't 'rescue' it. The underlying TCP connection is dead.

One solution is to entirely replace the AsyncWebsocket with a new one, but I haven't found how to close and destruct the old one.

Yes, this is exactly what you have to do. Just delete the WebSocket object - it will automatically close the connection if that connection is not used by another object.

xanderdunn commented 3 years ago

I see, thanks. The sockets are typically stored as std::shared_ptr<oatpp::websocket::AsyncWebSocket>, so the idea here is to set that to nullptr everywhere it's stored and that should be enough to destruct it? Then I can create a new socket and store it in all the relevant references.

lganzzzo commented 3 years ago

Right

xanderdunn commented 3 years ago

Thanks, I believe it's working as desired now.