public MessageTask(ROSBridgeSubscriber subscriber, ROSMessage msg)
 {
     this.subscriber = subscriber;
     this.msg        = msg;
 }
 public abstract string ToMessage(ROSMessage message);
        private void OnMessage(string message)
        {
            if ((message != null) && !message.Equals(string.Empty))
            {
                Topper topper = new Topper(message);

                string op = topper.op;

                if ("publish".Equals(op))                  // Topic
                {
                    string topic     = topper.topic;
                    string msgParams = string.Empty;

                    // if we have message parameters, parse them
                    Match      match = Regex.Match(message, @"""msg""\s*:\s*({.*}),");
                    ROSMessage msg   = null;

                    if (match.Success)
                    {
                        msgParams = match.Groups[1].Value;
                    }

                    foreach (var sub in this.subscribers)
                    {
                        // only consider subscribers with a matching topic
                        if (topic != sub.Key.Topic)
                        {
                            continue;
                        }

                        msg = sub.Key.ParseMessage(msgParams);
                        MessageTask newTask = new MessageTask(sub.Key, msg);

                        lock (this.lockMsgQueue)
                        {
                            this.msgQueue[topic].Enqueue(newTask);
                        }
                    }
                }
                else if (Regex.IsMatch(message, @"""op""\s*:\s*""call_service"""))                 // Service
                {
                    ServiceHeader header = new ServiceHeader(message);

                    foreach (var srv in this.serviceProviders)
                    {
                        if (srv.Key.Name == header.service)
                        {
                            ServiceArgs args = null;
//							ServiceResponse response = null;

                            // if we have args, parse them (args are optional on services, though)
                            Match match = Regex.Match(message, @"""args""\s*:\s*({.*}),");
                            if (match.Success)
                            {
                                args = srv.Key.ParseRequest(match.Groups[1].Value);
                            }

                            // add service request to queue, to be processed later in Render()
                            lock (this.lockMsgQueue)
                            {
                                this.svcQueue[srv.Key.Name].Enqueue(new ServiceTask(srv.Key, args, header.id));
                            }

                            break;                             // there should only be one server for each service
                        }
                    }
                }
                else
                {
                    Debug.LogWarning("Unhandled message:\n" + message);
                }
            }
            else
            {
                Debug.Log("Got an empty message from the web socket");
            }
        }