Skip to content

Commit aece27d

Browse files
committed
[websocket - client] Clean close on write error to allow reconnect
1 parent 804d242 commit aece27d

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/websockets/libwebsockets/LibWebsocketClient.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,15 @@ void LibWebsocketClient::process()
241241
sigaddset(&set, SIGPIPE);
242242
pthread_sigmask(SIG_BLOCK, &set, NULL);
243243

244+
// Need to ensure that the context is still valid when a user callback
245+
// has called disconnect() function
246+
lws_context* context = m_context;
247+
244248
// Event loop
245249
int ret = 0;
246250
while (!m_end && (ret >= 0))
247251
{
248-
ret = lws_service(m_context, 0);
252+
ret = lws_service(context, 0);
249253
}
250254
if (!m_end)
251255
{
@@ -255,7 +259,7 @@ void LibWebsocketClient::process()
255259

256260
// Destroy context
257261
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // Ensure disconnect caller is joining
258-
lws_context_destroy(m_context);
262+
lws_context_destroy(context);
259263
}
260264

261265
/** @brief libwebsockets connection callback */
@@ -391,15 +395,17 @@ int LibWebsocketClient::eventCallback(struct lws* wsi, enum lws_callback_reasons
391395
{
392396
if (lws_write(wsi, msg->payload, msg->size, LWS_WRITE_TEXT) < static_cast<int>(msg->size))
393397
{
394-
// Error
395-
client->disconnect();
396-
client->m_listener->wsClientError();
398+
// Error, close the socket
397399
error = true;
398400
}
399401

400402
// Free message memory
401403
delete msg;
402404
}
405+
if (error)
406+
{
407+
return -1;
408+
}
403409
}
404410
break;
405411

0 commit comments

Comments
 (0)