예제 #1
0
    async static void ReadWS()
    {
        var buffer = new ArraySegment <byte>(new byte[2048]);

        do
        {
            WebSocketReceiveResult result;
            using (var ms = new MemoryStream()) {
                do
                {
                    result = await socket.ReceiveAsync(buffer, CancellationToken.None);

                    ms.Write(buffer.Array, buffer.Offset, result.Count);
                } while (!result.EndOfMessage);

                if (result.MessageType == WebSocketMessageType.Close)
                {
                    break;
                }

                ms.Seek(0, SeekOrigin.Begin);
                using (var reader = new StreamReader(ms, Encoding.UTF8)) {
                    string rcvMsg = await reader.ReadToEndAsync();

                    IncomingNetworkMessage msg = JsonUtility.FromJson <IncomingNetworkMessage>(rcvMsg);
                    if (Array.IndexOf(ignoreActions, msg.action) == -1)
                    {
                        Debug.Log("Incoming ---> " + msg.action + " --- DATA:  " + msg.data);
                    }
                    queue.Enqueue(msg);
                }
            }
        } while (true);
    }
예제 #2
0
    void Update()
    {
        IncomingNetworkMessage msg = GetMessage();

        while (msg != null)
        {
            // if (Array.IndexOf(ignoreActions, msg.action) == -1) {
            //     Debug.Log("Triggering ---> " + msg.action + " --- DATA:  " + msg.data);
            // }
            EventManager.TriggerEvent(msg.action, msg.data);
            msg = GetMessage();
        }
    }
예제 #3
0
    static void ReadSocket()
    {
        do
        {
            //IPEndPoint object will allow us to read datagrams sent from any source.
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            // Blocks until a message returns on this socket from a remote host.
            Byte[] receiveBytes = socket.Receive(ref RemoteIpEndPoint);

            string rcvMsg = Encoding.ASCII.GetString(receiveBytes);
            IncomingNetworkMessage msg = JsonUtility.FromJson <IncomingNetworkMessage>(rcvMsg);
            if (Array.IndexOf(ignoreActions, msg.action) == -1)
            {
                Debug.Log("Incoming ---> " + msg.action + " --- DATA:  " + msg.data);
            }
            queue.Enqueue(msg);
        } while (true);
    }