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

Support unsubscribing from a topic #107

Merged
merged 4 commits into from
Apr 12, 2021
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
26 changes: 26 additions & 0 deletions plugins/websocket_server/WebsocketServer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ int rootCallback(struct lws *_wsi,
igndbg << "LWS_CALLBACK_HTTP\n";
return httpCallback(_wsi, _reason, _user, _in, _len);
break;

// Publish outboud messages
case LWS_CALLBACK_SERVER_WRITEABLE:
{
Expand Down Expand Up @@ -858,7 +859,32 @@ void WebsocketServer::OnMessage(int _socketId, const std::string &_msg)
this->node.Subscribe(frameParts[1],
&WebsocketServer::OnWebsocketSubscribedImageMessage, this);
}
else if (frameParts[0] == "unsub")
{
igndbg << "Unsubscribe request for topic[" << frameParts[1] << "]\n";
std::map<std::string, std::set<int>>::iterator topicConnectionIter =
this->topicConnections.find(frameParts[1]);

if (topicConnectionIter != this->topicConnections.end())
{
// Remove from the topic connections map
topicConnectionIter->second.erase(_socketId);

// Only unsubscribe from the Ignition Transport topic if there are no
// more websocket connections.
if (topicConnectionIter->second.empty())
{
igndbg << "Unsubscribing from Ignition Transport Topic["
<< frameParts[1] << "]\n";
this->node.Unsubscribe(frameParts[1]);
}
}
else
{
ignwarn << "The websocket server is not subscribed to topic["
<< frameParts[1] << "]. Unable to unsubscribe from the topic\n";
}
}
}

//////////////////////////////////////////////////
Expand Down
8 changes: 5 additions & 3 deletions plugins/websocket_server/WebsocketServer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ namespace ignition
/// 5. "protos": Get a string containing all the protobuf
/// definitions, and
/// 6. "particle_emitters": Get the list of particle emitters.
/// definitions.
/// 7. "unsub": Unsubscribe from the topic in the `topic_name` component
///
/// The `topic_name` component is mandatory for the "sub" and "pub"
/// operations. If present, it must be the name of an Ignition Transport
/// topic.
/// The `topic_name` component is mandatory for the "sub", "pub", and
/// "unsub" operations. If present, it must be the name of an Ignition
/// Transport topic.
///
/// The `message_type` component is mandatory for the "pub" operation. If
/// present it names the Ignition Message type, such as
Expand Down