oatpp / oatpp-websocket

oatpp-websocket submodule.

Home Page:https://oatpp.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: Recommendations on Reconnecting?

xanderdunn opened this issue · comments

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:

  • The server closes the connection, calling onClose/onFinishListen
  • An error occurs, calling handleError. This could be an Invalid communication state or an Unknown frame error.

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.

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.

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.

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