diff --git a/plugins/websocket_server/WebsocketServer.cc b/plugins/websocket_server/WebsocketServer.cc index 2bdf8c51..144d0221 100644 --- a/plugins/websocket_server/WebsocketServer.cc +++ b/plugins/websocket_server/WebsocketServer.cc @@ -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: { @@ -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>::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"; + } + } } ////////////////////////////////////////////////// diff --git a/plugins/websocket_server/WebsocketServer.hh b/plugins/websocket_server/WebsocketServer.hh index 853dcdfa..b1cb0f19 100644 --- a/plugins/websocket_server/WebsocketServer.hh +++ b/plugins/websocket_server/WebsocketServer.hh @@ -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