Skip to content

Commit

Permalink
Add pong delegate support for websoket connection and resource (#2156)
Browse files Browse the repository at this point in the history
Add ability to call delegate on pong frame reception. This is useful for
closing stalled connections.
Remove websocket state change before calling close() in
WebsocketConnection destructor. Without this change if connection was
removed by WebsocketRecource, onDisconnect delegate for Websocket
connection will not run.
  • Loading branch information
avr39-ripe authored Nov 26, 2020
1 parent 08c9eb1 commit 7b6a003
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Sming/Core/Network/Http/Websocket/WebsocketConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ int WebsocketConnection::staticOnControlEnd(void* userData)
connection->send(connection->controlFrame.payload, connection->controlFrame.payloadLength, WS_FRAME_PONG);
}

if(connection->controlFrame.type == WS_FRAME_PONG && connection->wsPong) {
connection->wsPong(*connection);
}
return WS_OK;
}

Expand Down
11 changes: 9 additions & 2 deletions Sming/Core/Network/Http/Websocket/WebsocketConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class WebsocketConnection

virtual ~WebsocketConnection()
{
state = eWSCS_Closed;
close();
}

Expand Down Expand Up @@ -214,7 +213,14 @@ class WebsocketConnection
{
wsBinary = handler;
}

/**
* @brief Sets the callback handler to be called when pong reply received
* @param handler
*/
void setPongHandler(WebsocketDelegate handler)
{
wsPong = handler;
}
/**
* @brief Sets the callback handler to be called before closing a websocket connection
* @param handler
Expand Down Expand Up @@ -298,6 +304,7 @@ class WebsocketConnection
WebsocketDelegate wsConnect = nullptr;
WebsocketMessageDelegate wsMessage = nullptr;
WebsocketBinaryDelegate wsBinary = nullptr;
WebsocketDelegate wsPong = nullptr;
WebsocketDelegate wsDisconnect = nullptr;

void* userData = nullptr;
Expand Down
1 change: 1 addition & 0 deletions Sming/Core/Network/Http/Websocket/WebsocketResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ int WebsocketResource::checkHeaders(HttpServerConnection& connection, HttpReques
socket->setBinaryHandler(wsBinary);
socket->setMessageHandler(wsMessage);
socket->setConnectionHandler(wsConnect);
socket->setPongHandler(wsPong);
socket->setDisconnectionHandler(wsDisconnect);
if(!socket->bind(request, response)) {
debug_w("Not a valid WebsocketRequest?");
Expand Down
6 changes: 6 additions & 0 deletions Sming/Core/Network/Http/Websocket/WebsocketResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class WebsocketResource : public HttpResource
wsBinary = handler;
}

void setPongHandler(WebsocketDelegate handler)
{
wsPong = handler;
}

void setDisconnectionHandler(WebsocketDelegate handler)
{
wsDisconnect = handler;
Expand All @@ -60,5 +65,6 @@ class WebsocketResource : public HttpResource
WebsocketDelegate wsConnect = nullptr;
WebsocketMessageDelegate wsMessage = nullptr;
WebsocketBinaryDelegate wsBinary = nullptr;
WebsocketDelegate wsPong = nullptr;
WebsocketDelegate wsDisconnect = nullptr;
};

0 comments on commit 7b6a003

Please sign in to comment.