Search code examples
c#c++cpprest-sdk

How to close C++ REST Sdk websocket?


I'm using cpprestsdk on the client and .net core 2.1 on the server side. Everything works except the closing part.

// C++
web::websockets::client::websocket_callback_client _client;
//connecting and working with websocket...
_client.close().wait();

// C#
while (!Socket.CloseStatus.HasValue)
{
    //sending/reciving data
}
await Socket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Connection closed", CancellationToken.None);

The issue is that the _client.close().wait(); never exits. The server gets the close request and calls CloseOutputAsync successfully. And I can't figure out why it never exits _client.close().wait();. It looks like there is some issue with the handshake between C++ and .net core implementations and didn't manage to make a workaround. Is there any way of forcing _client.close().wait(); to close the connection and do not wait for the handshake part from the server? Or is there is something wrong with the server code of closing a web socket?


Solution

  • It was my own mistake. I have set the _client.set_close_handler(...) which use lock_guard. This cases a deadlock since this mutex was locked during the close() call.