コード例 #1
0
        /// <summary>
        /// Add a publisher to this connection. There can be many publishers.
        /// </summary>
        /// <typeparam name="Tpub">Publisher type to advertise</typeparam>
        /// <param name="topic">Topic to advertise on</param>
        /// <returns>A publisher which can be used to broadcast data on the given topic</returns>
        public ROSBridgePublisher <Tmsg> Advertise <Tmsg>(string topic, uint queueSize = 0) where Tmsg : ROSMessage
        {
            ROSBridgePublisher <Tmsg> publisher = (ROSBridgePublisher <Tmsg>)Activator.CreateInstance(typeof(ROSBridgePublisher <Tmsg>), new object[] { topic, queueSize });

            publisher.SetConnection(this);

            this.publishers.Add(publisher);

            if (this.IsConnected)
            {
                this.webSocket.Send(ROSBridgeMsg.AdvertiseTopic(publisher.Topic, publisher.Type));
            }

            return(publisher);
        }
コード例 #2
0
        /// <summary>
        /// Connect to the remote ros environment.
        /// </summary>
        public void Connect()
        {
            if (this.IsConnected)
            {
                return;
            }

            string url = "ws://" + this.host + ":" + this.port;

            if (!CanConnect(url))
            {
                throw new Exception("Cannot connect. url=" + url);
            }

            this.webSocket = new WebSocket(url);

            this.webSocket.OnOpen    += (sender, eventArgs) => { Debug.Log("WebSocket Open  url=" + url); };
            this.webSocket.OnMessage += (sender, eventArgs) => this.OnMessage(eventArgs.Data);
            this.webSocket.OnError   += (sender, eventArgs) => { Debug.Log("WebSocket Error Message: " + eventArgs.Message); };
            this.webSocket.OnClose   += (sender, eventArgs) => { Debug.Log("WebSocket Close"); };

//			this.webSocket.Connect();
            this.webSocket.ConnectAsync();

            DateTime startTime = DateTime.Now;

            while (this.webSocket.ReadyState != WebSocketState.Open)
            {
                if ((DateTime.Now - startTime).TotalMilliseconds > ConnectionTimeOut)
                {
                    AddCannotConnectUrlList(url);

                    SIGVerseLogger.Error("Failed to connect. IP=" + this.host + ", Port=" + this.port + "  (Time out)");
                    throw new Exception("Failed to connect. IP=" + this.host + ", Port=" + this.port + "  (Time out)");
                }

                Thread.Sleep(100);
            }

            if (!this.webSocket.IsAlive)
            {
                Debug.Log("Error: Connection was faild.");
            }
            else
            {
                Debug.Log("Connected to ROSbridge server");

                foreach (var sub in this.subscribers)
                {
                    this.webSocket.Send(ROSBridgeMsg.Subscribe(sub.Key.Topic, sub.Key.Type));
                    Debug.Log("Sending: " + ROSBridgeMsg.Subscribe(sub.Key.Topic, sub.Key.Type));
                }
                foreach (ROSBridgePublisher pub in this.publishers)
                {
                    this.webSocket.Send(ROSBridgeMsg.AdvertiseTopic(pub.Topic, pub.Type));
                    Debug.Log("Sending " + ROSBridgeMsg.AdvertiseTopic(pub.Topic, pub.Type));
                }
                foreach (var srv in this.serviceProviders)
                {
                    this.webSocket.Send(ROSBridgeMsg.AdvertiseService(srv.Key.Name, srv.Key.Type));
                    Debug.Log("Sending: " + ROSBridgeMsg.AdvertiseService(srv.Key.Name, srv.Key.Type));
                }

                this.isConnected = true;

                foreach (ROSBridgePublisher pub in this.publishers)
                {
                    pub.CreatePublishingThread();
                }
            }
        }