From cbb46b494fc0f4170eb2c3b76b18442488bf2c9d Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 8 Oct 2019 22:59:22 +0200 Subject: [PATCH] Reset timeout on websocket send and publishes --- src/WebSocket.h | 6 ++++++ src/WebSocketContextData.h | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/WebSocket.h b/src/WebSocket.h index 97ef4cd..83c5c60 100644 --- a/src/WebSocket.h +++ b/src/WebSocket.h @@ -55,6 +55,12 @@ public: /* Send or buffer a WebSocket frame, compressed or not. Returns false on increased user space backpressure. */ bool send(std::string_view message, uWS::OpCode opCode = uWS::OpCode::BINARY, bool compress = false) { + /* Every send resets the timeout */ + WebSocketContextData *webSocketContextData = (WebSocketContextData *) us_socket_context_ext(SSL, + (us_socket_context_t *) us_socket_context(SSL, (us_socket_t *) this) + ); + AsyncSocket::timeout(webSocketContextData->idleTimeout); + /* Transform the message to compressed domain if requested */ if (compress) { WebSocketData *webSocketData = (WebSocketData *) Super::getAsyncSocketData(); diff --git a/src/WebSocketContextData.h b/src/WebSocketContextData.h index 1fc117c..dc28ae2 100644 --- a/src/WebSocketContextData.h +++ b/src/WebSocketContextData.h @@ -47,10 +47,11 @@ struct WebSocketContextData { /* Each websocket context has a topic tree for pub/sub */ TopicTree topicTree; - WebSocketContextData() : topicTree([](Subscriber *s, std::string_view data) -> int { + WebSocketContextData() : topicTree([this](Subscriber *s, std::string_view data) -> int { /* We rely on writing to regular asyncSockets */ auto *asyncSocket = (AsyncSocket *) s->user; + asyncSocket->timeout(this->idleTimeout); asyncSocket->write(data.data(), data.length()); /* Reserved, unused */