Robin Lew
Robin Lew

Reputation: 483

How to detect disconnection in websockt by using boost::beast?

I know I can use the error code in async_write, async_read to check the disconnection.

However, in my situation, after I receiving something, I cannot write back immediately (or write back may never happens).

During the time between reading and writing, how can I check the client disconnetion exception?

Upvotes: 3

Views: 2235

Answers (1)

Vinnie Falco
Vinnie Falco

Reputation: 5353

I know I can use the error code in async_write, async_read to check the disconnection.

Actually only async_read will inform you if the other end closed the connection gracefully.

During the time between reading and writing, how can I check the client disconnetion exception?

You can't. In fact, it is not always even possible to know if the other end closed the connection, even if you are calling async_read. You need to use a timer to time-out the connection if there has been no activity for a while. You can use the ping function of the websocket stream if too much time elapses with no activity. This is demonstrated in the advanced server examples.

Upvotes: 5

Related Questions