public CmdInputAction(short t, long aid, byte action, byte not, long to = 0) { cmdType = t; switch (not) { case MsgBroadcastType.Peer: wp = PacketFunc.CreatePeerPacket(to); break; case MsgBroadcastType.Broadcast: wp = PacketFunc.CreateBroadcastPacket(true); break; case MsgBroadcastType.BroadcastNot: wp = PacketFunc.CreateBroadcastPacket(false); break; default: wp = PacketFunc.CreateBroadcastPacket(true); break; } wp.WriteInt16(MsgInsideDef.MsgInputAction); wp.WriteInt8(action); wp.WriteInt64(aid); }
public CmdChatText(short t, string msg, bool not) { cmdType = t; wp = PacketFunc.CreateBroadcastPacket(not); //广播是否包括自己 wp.WriteInt16(MsgInsideDef.MsgChatText); wp.WriteString(msg); }
/* Register a function for a given packet code (0 .. 249). * The packet code is the value of the first byte of the packet data stream. */ public void SetPacketDispatchFunction(byte code, PacketFunc func) { if (code >= 250) { throw new InvalidOperationException("Byte codes 250 through 255 are reserved by highscores."); } pf[code] = func; }
IEnumerator ping() { while (keepPing && true) { WritePacket wp = PacketFunc.CreateNullPacket(); client.Send(wp.ToArray()); yield return(new WaitForSeconds(8)); } }
//处理服务端消息 void onMessage(WebSocket ws, byte[] message) { ReadPacket rp = PacketFunc.CreateReadPacket(message); //包头 byte t = rp.ReadInt8(); if (t == MsgOutsideDef.MsgNull) { return; } //发送者ID,广播为0 long clientID = rp.ReadInt64(); switch (t) { case MsgOutsideDef.MsgPeer: //单播消息 short gameType = rp.ReadInt16(); foreach (Command cmd in responseCommands) { if (cmd.Type() == gameType) { cmd.Handle(this, rp); break; } } break; case MsgOutsideDef.MsgBroadcast: //广播消息 byte _ = rp.ReadInt8(); gameType = rp.ReadInt16(); foreach (Command cmd in responseCommands) { if (cmd.Type() == gameType) { cmd.Handle(this, rp); break; } } break; case MsgOutsideDef.MsgRoomJoin: //加入房间 long joinID = rp.ReadInt64(); bool canAdd = true; foreach (var id in ids) { if (id == joinID) { canAdd = false; break; } } if (canAdd) { ids.Add(joinID); if (statusCallBack != null) { statusCallBack(NetStatusType.Join, this, joinID); } } Debug.Log("Room join id: " + joinID.ToString()); break; case MsgOutsideDef.MsgRoomLeave: //离开房间 long leaveID = rp.ReadInt64(); ids.Remove(leaveID); if (statusCallBack != null) { statusCallBack(NetStatusType.Leave, this, leaveID); } Debug.Log("Room leave id: " + leaveID.ToString()); break; case MsgOutsideDef.MsgRoomIds: //获得房间ID列表 myID = rp.ReadInt64(); Debug.Log("myID: " + myID.ToString()); ids = new List <long>(); ids.Add(myID); long otherID; while ((otherID = rp.ReadInt64()) != 0) { ids.Add(otherID); } if (statusCallBack != null) { statusCallBack(NetStatusType.Ids, this); } Debug.Log("Room id count: " + ids.ToArray().Length.ToString()); break; } }