private void Run() { _ws = new WebSocket(_host + ":" + _port); _ws.OnMessage += (sender, e) => this.OnMessage(e.Data); _ws.OnError += _ws_OnError; _ws.OnOpen += _ws_OnOpen; _ws.Connect(); while (!connected) { Thread.Sleep(10); } foreach (var sub in _subscribers) { _ws.Send(ROSBridgeMsg.Subscribe(sub.Key.topic, sub.Key.type)); Debug.Log("Sending: " + ROSBridgeMsg.Subscribe(sub.Key.topic, sub.Key.type)); } foreach (var p in _publishers) { _ws.Send(ROSBridgeMsg.AdvertiseTopic(p.topic, p.type)); Debug.Log("Sending " + ROSBridgeMsg.AdvertiseTopic(p.topic, p.type)); } foreach (var srv in _serviceServers) { _ws.Send(ROSBridgeMsg.AdvertiseService(srv.Key.topic, srv.Key.type)); Debug.Log("Sending: " + ROSBridgeMsg.AdvertiseService(srv.Key.topic, srv.Key.type)); } while (_connected) { Thread.Sleep(10); } }
private void Run() { _ws = new WebSocket(_host + ":" + _port); // _ws.Compression = CompressionMethod.Deflate; _ws.Log.Level = LogLevel.Trace; // _ws.Log.File = "socket.log"; _ws.OnError += (sender, e) => { UnityEngine.Debug.LogError("Error: " + e.Message); }; _ws.OnClose += (sender, e) => { UnityEngine.Debug.Log("Connection closed: " + e.Reason); }; _ws.OnMessage += (sender, e) => this.OnMessage(e.Data); _ws.Connect(); if (_ws != null) { foreach (Type p in _subscribers) { _ws.Send(ROSBridgeMsg.Subscribe(GetMessageTopic(p), GetMessageType(p))); UnityEngine.Debug.Log("Sending " + ROSBridgeMsg.Subscribe(GetMessageTopic(p), GetMessageType(p))); } foreach (Type p in _publishers) { _ws.Send(ROSBridgeMsg.Advertise(GetMessageTopic(p), GetMessageType(p))); UnityEngine.Debug.Log("Sending " + ROSBridgeMsg.Advertise(GetMessageTopic(p), GetMessageType(p))); } } while (_applicationIsPlaying) { Thread.Sleep(1000); } }
/// <summary> /// Add a subscriber callback to this connection. There can be many subscribers. /// </summary> /// <typeparam name="Tmsg">Message type used in the callback</typeparam> /// <param name="sub">Subscriber</param> /// <param name="callback">Method to call when a message matching the given subscriber is received</param> public ROSBridgeSubscriber <Tmsg> Subscribe <Tmsg>(string topic, ROSMessageCallback <Tmsg> callback, uint queueSize = 0) where Tmsg : ROSBridgeLib.ROSMessage, new() { MessageCallback CB = (ROSMessage msg) => { Tmsg message = msg as Tmsg; callback(message); }; var getMessageType = typeof(Tmsg).GetMethod("GetMessageType"); if (getMessageType == null) { Debug.LogError("Could not retrieve method GetMessageType() from " + typeof(Tmsg).ToString()); return(null); } string messageType = (string)getMessageType.Invoke(null, null); if (messageType == null) { Debug.LogError("Could not retrieve valid message type from " + typeof(Tmsg).ToString()); return(null); } ROSBridgeSubscriber <Tmsg> sub = new ROSBridgeSubscriber <Tmsg>(topic, messageType); _subscribers.Add(sub, CB); _msgQueue.Add(sub.topic, new RenderQueue <MessageTask>(queueSize)); if (connected) { _ws.Send(ROSBridgeMsg.Subscribe(sub.topic, sub.type)); } return(sub); }
private void Run() { _ws = new WebSocket(_host + ":" + _port); _ws.OnMessage += (sender, e) => this.OnMessage(e.Data); //_ws.CloseAsync(); _ws.Connect(); foreach (Type p in _subscribers) { _ws.Send(ROSBridgeMsg.Subscribe(GetMessageTopic(p), GetMessageType(p))); if (_debug) { Debug.Log("Sending " + ROSBridgeMsg.Subscribe(GetMessageTopic(p), GetMessageType(p))); } } foreach (Type p in _publishers) { _ws.Send(ROSBridgeMsg.Advertise(GetMessageTopic(p), GetMessageType(p))); if (_debug) { Debug.Log("Sending " + ROSBridgeMsg.Advertise(GetMessageTopic(p), GetMessageType(p))); } } while (true) { Thread.Sleep(10000); } }
// <Changed> /** * Add a subscriber callback to this connection. There can be many subscribers. */ public void AddSubscriber(Type subscriber) { IsValidStaticSubscriber(subscriber); string topicName = (string)subscriber.GetMethod("GetMessageTopic", BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Invoke(null, new object[] { }); List <Type> topic_subs; if (static_subscribers.TryGetValue(topicName, out topic_subs)) { if (!topic_subs.Contains(subscriber)) { topic_subs.Add(subscriber); } } else { topic_subs = new List <Type>(2); topic_subs.Add(subscriber); static_subscribers.Add(topicName, topic_subs); } if (_connected) { _ws.Send(ROSBridgeMsg.Subscribe(topicName, GetMessageType(subscriber))); Debug.Log("Sending " + ROSBridgeMsg.Subscribe(topicName, GetMessageType(subscriber))); } }
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))); } foreach (Pair <Type, String> p in _publishersNew) { _ws.Send(ROSBridgeMsg.Advertise(p.Second, GetMessageType(p.First))); Debug.Log("Sending " + ROSBridgeMsg.Publish(p.Second, GetMessageType(p.First))); } while (true) { Thread.Sleep(1000); } }
private void Run() { #if UNITY_EDITOR _ws = new WebSocket(_host + ":" + _port); _ws.OnMessage += (sender, e) => this.OnMessage(e.Data); //TODO connecting takes too long _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))); } _connected = true; while (true) { Thread.Sleep(1000); } #endif }
public void AddSubscriber(ROSBridgeSubscriber subscriber) { _subscribers.Add(subscriber); if (IsConnected && !IsDisconnecting) { WebSocket.Send(ROSBridgeMsg.Subscribe(subscriber.GetMessageTopic(), subscriber.GetMessageType())); } }
/** * Add a subscriber callback to this connection. There can be many subscribers. */ public void AddSubscriber(Type subscriber) { IsValidSubscriber(subscriber); _subscribers.Add(subscriber); if (_running) { _ws.Send(ROSBridgeMsg.Subscribe(GetMessageTopic(subscriber), GetMessageType(subscriber))); } }
private void Run(Action <string, bool> callback) { try { WebSocket = new WebSocket(_host + ":" + _port); WebSocket.OnMessage += (sender, e) => this.OnMessage(e.Data); WebSocket.Connect(); if (callback != null) { callback(_robotName, WebSocket.IsAlive); } if (!WebSocket.IsAlive) { return; } } catch (Exception e) { Debug.Log(e); return; } IsConnected = WebSocket.IsAlive; WebSocket.OnClose += (sender, args) => { if (OnDisconnect != null) { OnDisconnect(args.WasClean); } }; foreach (ROSBridgeSubscriber subscriber in _subscribers) { WebSocket.Send(ROSBridgeMsg.Subscribe(subscriber.GetMessageTopic(), subscriber.GetMessageType())); Debug.Log("Sending " + ROSBridgeMsg.Subscribe(subscriber.GetMessageTopic(), subscriber.GetMessageType())); } foreach (ROSBridgePublisher publisher in _publishers) { WebSocket.Send(ROSBridgeMsg.Subscribe(publisher.GetMessageTopic(), publisher.GetMessageType())); Debug.Log("Sending " + ROSBridgeMsg.Advertise(publisher.GetMessageTopic(), publisher.GetMessageType())); } while (true) { Thread.Sleep(1000); } }
/// <summary> /// The function run by the _myThread thread. Establishes WebSocket connection with the remote ROSBridge server. /// Performs all the topic subscriptions and advertisements calls made to this class prior to connection establishment. /// </summary> private void Run() { _ws = new WebSocket(_host + ":" + _port); _ws.OnMessage += (sender, e) => this.OnMessage(e.Data); _ws.Connect(); _connected = true; foreach (KeyValuePair <string, List <Type> > topic in static_subscribers) { _ws.Send(ROSBridgeMsg.Subscribe(topic.Key, GetMessageType(topic.Value[0]))); Debug.Log("Sending " + ROSBridgeMsg.Subscribe(topic.Key, GetMessageType(topic.Value[0]))); } foreach (KeyValuePair <string, List <ROSTopicSubscriber> > topic in subscribers) { if (!static_subscribers.ContainsKey(topic.Key)) { _ws.Send(ROSBridgeMsg.Subscribe(topic.Key, topic.Value[0].GetMessageType(topic.Key))); Debug.Log("Sending " + ROSBridgeMsg.Subscribe(topic.Key, topic.Value[0].GetMessageType(topic.Key))); } } foreach (KeyValuePair <string, List <Type> > topic in static_publishers) { _ws.Send(ROSBridgeMsg.Advertise(topic.Key, GetMessageType(topic.Value[0]))); Debug.Log("Sending " + ROSBridgeMsg.Advertise(topic.Key, GetMessageType(topic.Value[0]))); } foreach (KeyValuePair <string, string> topic in publishTopic_to_messageType) { if (!static_publishers.ContainsKey(topic.Key)) { _ws.Send(ROSBridgeMsg.Advertise(topic.Key, topic.Value)); Debug.Log("Sending " + ROSBridgeMsg.Advertise(topic.Key, topic.Value)); } } //while(true) { // Thread.Sleep (1000); //} }
// <Changed> /// <summary> /// Register a ROSTopicSubscriber to receive messages from a topic. /// </summary> /// <param name="topicName">The topic to receive messages from.</param> /// <param name="subscriber">The ROSTopicSubscriber object to receive the messages from that topic.</param> public void AddSubscriber(string topicName, ROSTopicSubscriber subscriber) { List <ROSTopicSubscriber> topic_subs; if (subscribers.TryGetValue(topicName, out topic_subs)) { if (!topic_subs.Contains(subscriber)) { topic_subs.Add(subscriber); } } else { topic_subs = new List <ROSTopicSubscriber>(2); topic_subs.Add(subscriber); subscribers.Add(topicName, topic_subs); } if (_connected) { _ws.Send(ROSBridgeMsg.Subscribe(topicName, subscriber.GetMessageType(topicName))); Debug.Log("Sending " + ROSBridgeMsg.Subscribe(topicName, subscriber.GetMessageType(topicName))); } }
private void Run() { _ws = new WebSocket(_host + ":" + _port); _ws.OnMessage += (sender, e) => this.OnMessage(e.Data); _ws.OnError += (sender, e) => OnError(sender, e); _ws.Connect(); CheckConnection(); _running = true; 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(10000); } }
//Successfull network connection handler on hololens #if !UNITY_EDITOR public void NetworkConnectedHandler(IAsyncAction asyncInfo, AsyncStatus status) { // Status completed is successful. if (status == AsyncStatus.Completed) { //Creating the writer that will be repsonsible to send a message through Rosbridge dataWriter = new DataWriter(messageWebSocket.OutputStream); //Connect to all topics in _subscribers and _publishers foreach (Type p in _subscribers) { dataWriter.WriteString(ROSBridgeMsg.Subscribe(GetMessageTopic(p), GetMessageType(p))); dataWriter.StoreAsync(); Debug.Log("Sending " + ROSBridgeMsg.Subscribe(GetMessageTopic(p), GetMessageType(p))); } foreach (Type p in _publishers) { dataWriter.WriteString(ROSBridgeMsg.Advertise(GetMessageTopic(p), GetMessageType(p))); dataWriter.StoreAsync(); Debug.Log("Sending " + ROSBridgeMsg.Advertise(GetMessageTopic(p), GetMessageType(p))); } _connected = true; } }
public void AddSubscriberOnline(Type subscriber, int throttle_rate) { AddSubscriber(subscriber); _ws.Send(ROSBridgeMsg.Subscribe(GetMessageTopic(subscriber), GetMessageType(subscriber), throttle_rate)); }
public void AddSubscriberOnline(Type subscriber) { AddSubscriber(subscriber); _ws.Send(ROSBridgeMsg.Subscribe(GetMessageTopic(subscriber), GetMessageType(subscriber))); }