restinio icon indicating copy to clipboard operation
restinio copied to clipboard

Send ping-pong frames from a websocket server to clients

Open jm130794 opened this issue 6 years ago • 4 comments

Hello,

I work with your example https://github.com/Stiffstream/restinio/blob/master/dev/sample/websocket/main.cpp

I want to send (for example every 30 seconds) ping-pong frames from my websocket server to clients to keep connections open or remove invalid connections from the registry.

What is the right method with Restinio?

Thanks,

Jean-Marc

jm130794 avatar Apr 11 '19 13:04 jm130794

Hi, Jean-Marc

Unfortunately, I have no time today to address your issue. I hope someone from our team can find some time to help you. But if not then I'll try to find an appropriate answer tomorrow.

eao197 avatar Apr 11 '19 15:04 eao197

Hi, Jean-Marc!

What is the right method with Restinio?

There is no such thing as "the right method" :)

I think there are at least two approaches which don't require any additional tools/libraries.

The first approach is to bind a timer to every WS-connection instance. I show this approach in this example.

There is a class actual_handler_t that contains all data related to ws-connection. An instance of that class also handles all WS-connection related operations (after upgrading to WS). It activates an Asio's timer on start and then reactivates this timer.

In my simple example, the only reaction to the expiration of timer is sending ping_frame. But you can place your own logic here. For example, you can add a timestamp field to actual_handler_t class. You will update this field on every incoming message and check this field when the timer expires.

If you have any question about my simple implementation feel free to ask me. And note that this example works on one thread. To use that approach in multithreaded case (e.g. when RESTinio is run on a thread pool) it is necessary to defend instances of actual_handler_t (because of timer-event and on_message can be called from different threads at the same time).

The second approach requires just one timer. You set this timer to expire every second (for example). Then you walk through your WS-connections registry and check the activity for every WS-connection. In that case, you should have a timestamp field in your WS-connection-related data object. And this field should be updated when WS-connection receives an incoming message.

Unfortunately, I have no time today to implement this approach in the form of a simple example.

eao197 avatar Apr 12 '19 14:04 eao197

Hello,

Great:) It should be added to the samples folder. I suppose it could be useful.

Thank you very much,

Jean-Marc

jm130794 avatar Apr 14 '19 14:04 jm130794

It should be added to the samples folder.

You're right. And we've updated one of the examples: https://bitbucket.org/sobjectizerteam/restinio-0.4/src/default/dev/sample/websocket_detailed/main.cpp There is a slightly modified version with killing WS-connection in the case if there are no incoming messages for too long.

eao197 avatar Apr 15 '19 16:04 eao197