/// <summary> /// Announces all publishers and subscribers which were previously added to the lists _publishers and _subscriers /// </summary> private void AnnouncePublishersAndSubscribers() { if (_running && _ws != null) { foreach (Type p in _subscribers) { string topic = GetMessageTopic(p); //only announce if not already known that we subscribed if (!m_SubscribedTopics.Contains(topic)) { Debug.Log("[ROS WEBSOCKET] Subscribing to " + topic); _ws.Send(ROSBridgeMsg.Subscribe(topic, GetMessageType(p))); } m_SubscribedTopics.Add(topic); } foreach (Type p in _publishers) { string topic = GetMessageTopic(p); //only announce new publisher if we didn't already announce one for this topic if (!m_AnnouncedTopics.Contains(topic)) { Debug.Log("[ROS WEBSOCKET] Advertising " + topic); _ws.Send(ROSBridgeMsg.Advertise(topic, GetMessageType(p))); } m_AnnouncedTopics.Add(topic); } } else { Debug.LogWarning("[ROS WEBSOCKET] COuld not advertise/ subscribe since websocket not running."); } }
private void Run() { _ws = new WebSocket(_host + ":" + _port); _ws.OnMessage += (sender, e) => this.OnMessage(e.Data); _ws.Connect(); foreach (Type p in _subscribers) { _ws.Send(ROSBridgeMsg.Subscribe(GetMessageTopic(p), GetMessageType(p))); Debug.Log("Sending " + ROSBridgeMsg.Subscribe(GetMessageTopic(p), GetMessageType(p))); } foreach (Type p in _publishers) { _ws.Send(ROSBridgeMsg.Advertise(GetMessageTopic(p), GetMessageType(p))); Debug.Log("Sending " + ROSBridgeMsg.Advertise(GetMessageTopic(p), GetMessageType(p))); } while (true) { Thread.Sleep(1000); } }
/** * Add a publisher to this connection. There can be many publishers. */ public void AddPublisher(Type publisher) { IsValidPublisher(publisher); string topic = GetMessageTopic(publisher); _publishers.Add(publisher); if (_running) { if (!m_AnnouncedTopics.Contains(topic)) { // only advertise if needed Debug.Log("[ROS WEBSOCKET] Adding publisher. Advertising " + topic); _ws.Send(ROSBridgeMsg.Advertise(topic, GetMessageType(publisher))); } //keep track of all publishers (no matter the topic or if the topic already exists ) m_AnnouncedTopics.Add(topic); } else { Debug.Log("[ROS WEBSOCKET] Could not advertise publisher. Websocket not running."); } }