예제 #1
0
        public void SendSocketMessage(ESocketEventType eventType, string data)
        {
            SocketMessageStruct mes = new SocketMessageStruct();

            mes.eventType = (int)eventType;
            mes.data      = data;
            try
            {
                string json = JsonUtility.ToJson(mes);
#if UNITY_EDITOR || UNITY_STANDALONE
                if (mWebSocketConnectionList != null)
                {
                    for (int i = 0; i < mWebSocketConnectionList.Count; i++)
                    {
                        if (mWebSocketConnectionList[i] != null && mWebSocketConnectionList[i].IsAvailable)
                        {
                            mWebSocketConnectionList[i].Send(json);
                        }
                    }
                }
#elif UNITY_ANDROID || UNITY_IPHONE
                if (mClientSocket != null)
                {
                    mClientSocket.Send(json);
                }
#endif
            }
            catch (Exception e)
            {
                Debug.Log("发送消息解析出错     error:" + e.Message);
            }
        }
예제 #2
0
 private void ConnectToServer(string path, int port)
 {
     mClientSocket         = new WebSocket("ws://" + path + ":" + port.ToString());
     mClientSocket.Opened += (sender, e) =>
     {
         Debug.Log("Connect to " + path);
     };
     mClientSocket.Closed += (sender, e) =>
     {
         Debug.Log("Disconnect from " + path);
     };
     mClientSocket.MessageReceived += (sender, e) =>
     {
         //接受消息
         if (e != null)
         {
             try
             {
                 SocketMessageStruct message = JsonUtility.FromJson <SocketMessageStruct>(e.Message);
                 InvokeSocketEvent(message.eventType, message.data);
             }
             catch (Exception err)
             {
                 Debug.Log("Socket消息解析出错     error:" + err.Message);
             }
         }
     };
     mClientSocket.Open();
 }
예제 #3
0
 private void CreateLocalServer()
 {
     if (mLocalServer != null)
     {
         mLocalServer.RestartAfterListenError = true;
         mLocalServer.Start(socket =>
         {
             socket.OnOpen = () =>
             {
                 if (mWebSocketConnectionList != null)
                 {
                     mWebSocketConnectionList.Add(socket);
                 }
                 Debug.Log(socket.ConnectionInfo.Id + "Open");
             };
             socket.OnClose = () =>
             {
                 if (mWebSocketConnectionList != null)
                 {
                     mWebSocketConnectionList.Remove(socket);
                 }
                 Debug.Log(socket.ConnectionInfo.Id + "Close");
             };
             socket.OnMessage = Message =>
             {
                 //接受消息
                 try
                 {
                     SocketMessageStruct message = JsonUtility.FromJson <SocketMessageStruct>(Message);
                     InvokeSocketEvent(message.eventType, message.data);
                 }
                 catch (Exception err)
                 {
                     Debug.Log("Socket消息解析出错     error:" + err.Message);
                 }
             };
         });
         Debug.Log("WebSocketServer On Listen");
     }
 }