Exemplo n.º 1
0
    void Recv()
    {
        if (ws != null)
        {
            if (ws.CanRead)
            {
                if (ws.DataAvailable)
                {
                    Debug.Log("can read===================================");
                    Debug.Log(msgLenBufferIndex);
                    Debug.Log(msgLenBuffer.Length);
                    if (msgLenBufferIndex < msgLenBuffer.Length)
                    {
                        int r = ws.Read(msgLenBuffer, msgLenBufferIndex, msgLenBuffer.Length - msgLenBufferIndex);
                        Debug.Log(r);
                        msgLenBufferIndex += r;
                    }

                    if (msgLenBufferIndex == msgLenBuffer.Length)
                    {
                        int msgLen = BitConverter.ToInt32(msgLenBuffer, 0);
                        msgLen -= 4;

                        Debug.Log(msgLen);
                        if (recvBufferIndex < msgLen)
                        {
                            int r = ws.Read(recvBuffer, recvBufferIndex, msgLen - recvBufferIndex);
                            recvBufferIndex += r;
                        }
                        Debug.Log(recvBufferIndex);
                        if (recvBufferIndex == msgLen)
                        {
                            msgLenBufferIndex = 0;
                            recvBufferIndex   = 0;

                            byte[] newBuffer = new byte[msgLen];

                            for (int i = 0; i < newBuffer.Length; i++)
                            {
                                newBuffer[i] = recvBuffer[i];
                            }

                            mRecv = NetMessage.ParseFrom(newBuffer);

                            Debug.Log(mRecv.ToString());
                        }
                    }
                }
            }
        }
    }