Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pong delegate support for websoket connection and resource #2156

Merged
merged 1 commit into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to remove the change of state here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if close method got connection in eWSCS_Closed state it do not fire onDisconnect delegate and this is not good. close() method change state by itself, and as far as I understand changing state to eWSCS_Closed in close() method do not hurt anything.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void WebsocketConnection::close()
Here is it, on line 269 there is delegate call, but it inside if(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;
};