Ejemplo n.º 1
0
    // 将数据打包成2进制
    public void Send <T>(string eventName, T message)
    {
        var serializer = MessagePackSerializer.Get <T> ();
        var ms1        = new MemoryStream();

        serializer.Pack(ms1, message);

        if (status != SocketState.Connected)
        {
            return;
        }

        byte[] data       = ms1.ToArray();
        var    netByteBuf = new NetByteBuf(data.Length + 2);

        netByteBuf.WriteShort((short)(data.Length + 1));
        byte cmd = (byte)EventName.GetEventCmd(eventName);

        Debug.Log("begin send:" + eventName + " cmd " + cmd);

        netByteBuf.WriteByte(cmd);
        netByteBuf.WriteBytes(data);

        byte[] sendData = netByteBuf.GetRaw();
        sendId += 1;

        Debug.Log("begin send:" + sendId);
        clientSocket.BeginSend(sendData,
                               0,
                               sendData.Length,
                               SocketFlags.None,
                               new AsyncCallback(send_cb), sendId);

        // clientSocket.Send(sendData, sendData.Length, SocketFlags.None);
    }