/// <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); }
public void RemoveSubcriber(ROSBridgeSubscriber subscriber) { _subscribers.Remove(subscriber); if (IsConnected) { WebSocket.Send(ROSBridgeMsg.UnSubscribe(subscriber.GetMessageTopic())); } }
public void AddSubscriber(ROSBridgeSubscriber subscriber) { _subscribers.Add(subscriber); if (IsConnected && !IsDisconnecting) { WebSocket.Send(ROSBridgeMsg.Subscribe(subscriber.GetMessageTopic(), subscriber.GetMessageType())); } }
void Start() { ROSHost = "ws://" + ROSHost; ros = new ROSBridgeLib.ROSBridgeWebSocketConnection(ROSHost, ROSPort); pose_sub = ros.Subscribe <SimplePoseArray>("/unity/simple_bot/pose", OnNewPoseMsg); hand_sub = ros.Subscribe <ROSBridgeLib.simple_unity_bot.HandState>("/unity/simple_bot/hands", OnNewHandMsg); pose_pub = ros.Advertise <SimplePoseArray>("/unity/simple_bot/target_pose"); hand_pub = ros.Advertise <ROSBridgeLib.simple_unity_bot.HandState>("/unity/simple_bot/hand_targets"); ros.Connect(); }
/// <summary> /// Remove a subscriber callback from this connection. /// </summary> /// <param name="sub"></param> public void Unsubscribe(ROSBridgeSubscriber sub) { if (sub == null) { return; } _subscribers.Remove(sub); _msgQueue.Remove(sub.topic); if (connected) { _ws.Send(ROSBridgeMsg.UnSubscribe(sub.topic)); } }
public MessageTask(ROSBridgeSubscriber subscriber, ROSMessage msg) { _subscriber = subscriber; _msg = msg; }
public RenderTask(ROSBridgeSubscriber subscriber, string topic, ROSBridgeMsg msg) { _subscriber = subscriber; _topic = topic; _msg = msg; }