Ejemplo n.º 1
0
        void client_MqttMsgReceived(object sender, MqttMsgPublishEventArgs e)
        {
            Debug.Log("Received: " + System.Text.Encoding.UTF8.GetString(e.Message));

            /*
             * // Updates to UI need to be called on the main thread, but msgreceived callback is in a thread.
             * UnityMainThreadDispatcher.Instance().Enqueue(() => {
             *  monitor.AddLog(System.Text.Encoding.UTF8.GetString(e.Message));
             * });
             */
            string[] topicArray;
            topicArray = e.Topic.Split('/');

            // e.g. ToHost/FromDevice/2
            if (topicArray.Length == 3)
            {
                UnityMainThreadDispatcher.Instance().Enqueue(() => {
                    monitor.AddLog(String.Format("Host received message {0} from Device {1}", System.Text.Encoding.UTF8.GetString(e.Message), topicArray[2]));
                    GameObject tmp = Instantiate(devicePrefab, devicePanel);
                    tmp.GetComponent <Device>().SetDeviceID(topicArray[2]);
                });
            }



            switch (System.Text.Encoding.UTF8.GetString(e.Message))
            {
            case "Ignore":
                break;

            default:
                // Updates to UI need to be called on the main thread, but msgreceived callback is in a thread.
                UnityMainThreadDispatcher.Instance().Enqueue(() => {
                    //GameObject tmp = Instantiate(devicePrefab, devicePanel);
                    //tmp.GetComponent<Device>().DeviceID =
                });
                break;
            }
        }