From 66e2e94b18013aa4de33d5fb1e9338a541c04597 Mon Sep 17 00:00:00 2001 From: Nate Koenig Date: Mon, 5 Apr 2021 14:15:22 -0700 Subject: [PATCH 1/2] Support unsubscribing from a topic Signed-off-by: Nate Koenig --- plugins/websocket_server/WebsocketServer.cc | 26 +++++++++++++++++++++ plugins/websocket_server/WebsocketServer.hh | 11 +++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/plugins/websocket_server/WebsocketServer.cc b/plugins/websocket_server/WebsocketServer.cc index 6b535543..53271cae 100644 --- a/plugins/websocket_server/WebsocketServer.cc +++ b/plugins/websocket_server/WebsocketServer.cc @@ -761,6 +761,32 @@ void WebsocketServer::OnMessage(int _socketId, const std::string &_msg) this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); } + 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.extract(_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 484cdddb..b29046b0 100644 --- a/plugins/websocket_server/WebsocketServer.hh +++ b/plugins/websocket_server/WebsocketServer.hh @@ -85,13 +85,14 @@ namespace ignition /// 1. "sub": Subscribe to the topic in the `topic_name` component, /// 2. "pub": Publish a message from the Ignition Transport topic in /// the `topic_name` component, - /// 3. "topics": Get the list of available topics, and + /// 3. "topics": Get the list of available topics, /// 4. "protos": Get a string containing all the protobuf - /// definitions. + /// definitions, and + /// 5. "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 From 48a8f48114a0d483e1072039729119c03ad2dc53 Mon Sep 17 00:00:00 2001 From: Nate Koenig Date: Fri, 9 Apr 2021 10:06:53 -0700 Subject: [PATCH 2/2] extract to erase Signed-off-by: Nate Koenig --- plugins/websocket_server/WebsocketServer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/websocket_server/WebsocketServer.cc b/plugins/websocket_server/WebsocketServer.cc index 53271cae..ce003e41 100644 --- a/plugins/websocket_server/WebsocketServer.cc +++ b/plugins/websocket_server/WebsocketServer.cc @@ -770,7 +770,7 @@ void WebsocketServer::OnMessage(int _socketId, const std::string &_msg) if (topicConnectionIter != this->topicConnections.end()) { // Remove from the topic connections map - topicConnectionIter->second.extract(_socketId); + topicConnectionIter->second.erase(_socketId); // Only unsubscribe from the Ignition Transport topic if there are no // more websocket connections.