timum-viw / socket.io-client

A socket.io-client implementation for ESP8266 and Arduino
228 stars 90 forks source link

Add lifecycle handlers (connect, disconnect, etc) #21

Closed suyashkumar closed 6 years ago

suyashkumar commented 6 years ago

I propose that user-supplied lifecycle functions should be called on useful events like socket CONNECT and DISCONNECT events. This will allow users of this library to execute any application logic necessary when these events occur.

For example, when the socketio client disconnects and reconnects, there may be some application logic needed to reset state or renegotiate with the server (there will be a new session id issued upon the new connection as well).

I can go ahead and put a PR up for this soon--let me know if this is something that makes sense to you! I was thinking of something simple like:

typedef std::function<void()> lifecycleEventHandler;
...
SocketIoClient::onDisconnect(h lifecycleEventHandler);
SocketIoClient::onConnect(h lifecycleEventHandler);
timum-viw commented 6 years ago

Actually this is already implemented by emitting events 'connect' and 'disconnect' which you can subscribe to. Is that what you had in mind?

suyashkumar commented 6 years ago

Indeed! Ended up going that route, thanks @timum-viw!