boostorg / beast

HTTP and WebSocket built on Boost.Asio in C++11
http://www.boost.org/libs/beast
Boost Software License 1.0
4.36k stars 634 forks source link

async_close do not recv peer close frame #2916

Closed ahao1995 closed 3 weeks ago

ahao1995 commented 3 months ago

I use async_close to close the socket, but after 5min recv callback, have any method to force close the connection, and not wait the close frame may be I should call

std::optional<boost::beast::websocket::stream<
    boost::beast::ssl_stream<boost::beast::tcp_stream>>> ws_stream_;
//force close?
boost::beast::get_lowest_layer(*ws_stream_).cancel();

after that I want to do reconnect

ws_stream_.emplace(exec_, ctx_);

it is right? thanks

ashtum commented 3 months ago

Have you set a timeout on the stream? https://www.boost.org/doc/libs/1_85_0/libs/beast/doc/html/beast/using_websocket/timeouts.html

ahao1995 commented 3 months ago

Have you set a timeout on the stream? https://www.boost.org/doc/libs/1_85_0/libs/beast/doc/html/beast/using_websocket/timeouts.html

I do not use timeout, I want to close it quickly and reconnect, just like force close not graceful, is there some method?

ashtum commented 3 months ago

beast::get_lowest_layer(*ws_stream_).close(); will do it, or you can simply ignore the stream object and let it be destroyed.

ahao1995 commented 3 months ago

beast::get_lowest_layer(*ws_stream_).close(); will do it, or you can simply ignore the stream object and let it be destroyed.

you mean direct use ws_stream_.emplace(exec_, ctx_); let it destroy?

ashtum commented 3 months ago

Yes, that would have the same effect as closing the underlying socket.