Ejemplo n.º 1
0
 internal Action <Tout> Handler;                             // コールバック関数のハンドラーのデリゲートを生成
 public ServiceClientAsyncManager(string s, Action <Tout> h) // コンストラクタ
 {
     // オペレーションを設定
     service_name = s;
     request_data = new CallServiceMessage <Tin>();
     Handler      = h;
 }
Ejemplo n.º 2
0
        public void Connect()
        {
            if (!isConnected)
            {
                ws         = new WebSocket("ws://" + IPAddress + ":" + Port + "/");
                ws.OnOpen += (sender, e) =>
                {
                    Debug.Log("WebSocket Opened.");
                    isConnected = true;
                };

                ws.OnMessage += (sender, e) =>
                {
                    OperationMessage data = JsonUtility.FromJson <OperationMessage>(e.Data);
                    if (data.op == "publish")
                    {
                        SubscribeMessage msg = JsonUtility.FromJson <SubscribeMessage>(e.Data);
                        foreach (SubscribeManager sm in subscribeManagers)
                        {
                            if (msg.topic == sm.Topic)
                            {
                                if (!sm.IsRunning)
                                {
                                    sm.HandlerFunction(e.Data);
                                }
                            }
                        }
                    }

                    else if (data.op == "service_response")
                    {
                        ServiceResponseMessage msg = JsonUtility.FromJson <ServiceResponseMessage>(e.Data);
                        foreach (ServiceClientManager sc in serviceClientManagers)
                        {
                            if (msg.service == sc.service_name)
                            {
                                sc.ResponseFunction(e.Data);
                            }
                        }
                    }

                    else if (data.op == "call_service")
                    {
                        CallServiceMessage msg = JsonUtility.FromJson <CallServiceMessage>(e.Data);
                        foreach (var ss in serviceServerManagers)
                        {
                            if (msg.service == ss.service)
                            {
                                ss.HandlerFunction(e.Data);
                            }
                        }
                    }
                };

                ws.OnError += (sender, e) =>
                {
                    Debug.Log("WebSocket Error Message: " + e.Message);
                };

                ws.OnClose += (sender, e) =>
                {
                    Debug.Log("WebSocket Closed.");
                    isConnected = false;
                };

                if (IsConnectable())
                {
                    ws.Connect();
                }
            }
        }