private void Start() { pb.PlayerSnapShootMsg msg = new pb.PlayerSnapShootMsg(); msg.username = "******"; msg.playerId = 9000001; byte[] data = ProtobufSerializer.Serialize(msg); Debug.LogError("data len " + data.Length); ByteBuffer buffer = ByteBuffer.Allocate(1024); buffer.WriteShort(1001); buffer.WriteShort(1002); buffer.WriteShort(1003); buffer.WriteLong(10055555); buffer.WriteBytes(data); short s1 = buffer.ReadShort(); short s2 = buffer.ReadShort(); short s3 = buffer.ReadShort(); long l1 = buffer.ReadLong(); byte[] readData = new byte[buffer.ReadableBytes()]; buffer.ReadBytes(readData, 0, buffer.ReadableBytes()); Debug.LogError("readData len " + readData.Length); pb.PlayerSnapShootMsg dmsg = ProtobufSerializer.DeSerialize <pb.PlayerSnapShootMsg>(readData); Debug.LogError("user name " + dmsg.username); //socket receive bytes // }
private void Update() { if (Input.GetKeyDown(KeyCode.V)) { int HEAD_LEN = 18;//包头长度 short HEAD_FIX = 0x71ab; pb.PlayerSnapShootMsg msg = new pb.PlayerSnapShootMsg(); msg.playerId = 80000060; msg.username = "******"; byte[] data = ProtobufSerializer.Serialize <pb.PlayerSnapShootMsg>(msg); int len = data.Length; ByteBuffer buffer = ByteBuffer.Allocate(len + HEAD_LEN); buffer.WriteShort(HEAD_FIX); buffer.WriteShort((short)(HEAD_LEN + len)); buffer.WriteShort(12001); buffer.WriteLong(90000001001); buffer.WriteInt(0); buffer.WriteBytes(data); GameSocket.Instance.Send(buffer); Debug.LogError("发送一条消息给服务器1"); } }