/// <summary> /// 向玩家发送单个玩家更新状态 /// </summary> /// <param name="ip"></param> /// <param name="info"></param> public void SendUpdatePlayrInfo(string ip, RoomPlayerInfoMessage info) { byte[] bytes = RoomPlayerInfoMessage.GetBytes(info); NetworkMessage sendM = new NetworkMessage(10, NetworkTools.GetLocalIP(), bytes); SendMsg(ip, NetworkConstent.UDPClientPort, NetworkMessage.GetBytes(sendM)); }
public static byte[] GetBytes(RoomPlayerInfoMessage message) { byte[] name = Encoding.UTF8.GetBytes(message.playerName + "*" + message.playerIP); byte[] newBytes = new byte[name.Length + 2]; BitConverter.GetBytes(message.canStart).CopyTo(newBytes, 0); name.CopyTo(newBytes, 2); return(newBytes); }
/// <summary> /// 向玩家发送房间内所有玩家信息 /// </summary> /// <param name="ip"></param> public void SendPlayerInfosToClient(string ip) { foreach (var item in playerData.GetPlayersInfo().Keys) { byte[] bytes = RoomPlayerInfoMessage.GetBytes(playerData.GetPlayersInfo()[item]); NetworkMessage sendM = new NetworkMessage(15, NetworkTools.GetLocalIP(), bytes); SendMsg(ip, NetworkConstent.UDPClientPort, NetworkMessage.GetBytes(sendM)); } }
/// <summary> /// 想要加入的房间 /// </summary> /// <param name="ip"></param> public void SendAddRoom(string ip) { try { IPAddress.Parse(ip); Debug.Log("请求加入: " + ip); } catch (System.Exception) { Debug.LogError("请输入有效IP"); return; } byte[] info = RoomPlayerInfoMessage.GetBytes(new RoomPlayerInfoMessage("", false, NetworkTools.GetLocalIP())); SendMsg(ip, NetworkConstent.UDPServerPort, NetworkMessage.GetBytes(new NetworkMessage(7, NetworkTools.GetLocalIP(), info))); }
public void AddPlayerInfo(string ip, RoomPlayerInfoMessage info) { if (playerDic.Count >= 4) { return; } if (!playerDic.ContainsKey(ip)) { lock (playerDic) { playerDic.Add(ip, info); } } else { playerDic[ip] = info; } }
protected override void NetworkCallback(object message) { if (message.GetType().ToString().Equals("Network.NetworkMessage")) { Debug.Log("客户端接收"); NetworkMessage nm = (NetworkMessage)message; if (nm.type == 13) { PlayerInfoMessage info = PlayerInfoMessage.GetMessage(nm.message); RoomSingle.AddPlayer(info); //Debug.Log("开始游戏"); UIManager._Instance.CloseWindow(WindowName.WaitStartRoom); LoadSceneManager._Instance.LoadScene(SceneName.Mission1, StartGameCallBack); //NetworkManager._Instance.RemoveCallBack(13, GetNetworkMsgCallBack); } else if (nm.type == 10) { RoomPlayerInfoMessage info = RoomPlayerInfoMessage.GetMessage(nm.message); playerData.SetPlayerCanStart(info.playerIP, info.canStart); wait.UpdatePanel(playerData); } else if (nm.type == 15) { RoomPlayerInfoMessage info = RoomPlayerInfoMessage.GetMessage(nm.message); playerData.AddPlayerInfo(info.playerIP, info); wait.UpdatePanel(playerData); } else if (nm.type == 16) { RoomPlayerInfoMessage info = RoomPlayerInfoMessage.GetMessage(nm.message); playerData.RemovePlayerInfo(info.playerIP); RoomSingle.RemovePlayer(info.playerIP); wait.UpdatePanel(playerData); } else if (nm.type == 17) { PlayerInfoMessage info = PlayerInfoMessage.GetMessage(nm.message); RoomSingle.AddPlayer(info); playerData.SetPlayerCanStart(info.playerIP, true); wait.UpdatePanel(playerData); } } }
/// <summary> /// 向客户端发送可以加入信息 /// </summary> /// <param name="obj_arr"></param> void AddRoomMsgCallBack(params object[] obj_arr) { //房间最多4个人 if (playerData.GetPlayersInfo().Count < 4) { NetworkMessage m = (NetworkMessage)obj_arr[0]; RoomPlayerInfoMessage info = RoomPlayerInfoMessage.GetMessage(m.message); playerData.AddPlayerInfo(m.ip, info); NetworkManager._Instance.AddMessage(102, playerData); Debug.Log("房间加入玩家" + m.ip + " --- " + playerData.GetPlayersInfo().Count); byte[] bytes = NetworkMessage.GetBytes(new NetworkMessage(8, NetworkTools.GetLocalIP(), new byte[0])); SendMsg(m.ip, NetworkConstent.UDPClientPort, bytes); //向所有玩家发送加入信息 foreach (var item in playerData.GetPlayersInfo().Keys) { if (!item.Equals(NetworkTools.GetLocalIP())) { SendPlayerInfosToClient(item); } } } }