private void Update() { while (NetDatas.Count > 0) { NetData n = NetDatas[0]; try { switch (n.MessageType) { // 1 下线警告 case NetMessageType.Warning: Debug.LogWarning(n.ReadString()); break; case NetMessageType.Error: Debug.LogError(n.ReadString()); break; case NetMessageType.Heartbeat: int d = DateTime.Now.Millisecond - n.ReadInt(); Debug.Log(n.RemoteIP + "延迟:" + d); break; case NetMessageType.Data: break; case NetMessageType.GainServer: if (Net.Status == NetStatus.Server) { NetData g = new NetData(NetMessageType.SendServer); g.Write(NetServer.ServerName); g.Write(NetServer.PlayerName); g.Write(NetServer.IsPassword); g.Write(NetServer.ClientNumber); g.Write(NetServer.ClientMax); NetUdp.Send(g, n.RemoteIP); } break; case NetMessageType.SendServer: string ssn = n.ReadString(); string spn = n.ReadString(); IPEndPoint sip = n.RemoteIP; bool spd = n.ReadBool(); int scn = n.ReadInt(); int scm = n.ReadInt(); NetClient.Servers.Add(new NetServer.Info(ssn, spn, sip, spd, scn, scm)); break; case NetMessageType.Connecting: if (Net.Status == NetStatus.Server) { if (NetServer.ClientNumber >= NetServer.ClientMax) { NetData cw0 = new NetData(NetMessageType.Warning); cw0.Write("该服务器玩家数已满!"); NetUdp.Send(cw0, n.RemoteIP); } string cn = n.ReadString(); if (NetServer.IsPassword) { if (!n.IsRead()) { NetData cw1 = new NetData(NetMessageType.Warning); cw1.Write("该服务器需要密码访问!"); NetUdp.Send(cw1, n.RemoteIP); break; } string cp = n.ReadString(); if (NetServer.Password != cp) { NetData cw2 = new NetData(NetMessageType.Warning); cw2.Write("该服务器访问密码错误!"); NetUdp.Send(cw2, n.RemoteIP); break; } } if (NetServer.Clients.ContainsIP(n.RemoteIP)) { NetData cw3 = new NetData(NetMessageType.Warning); cw3.Write("请勿重复登录!"); NetUdp.Send(cw3, n.RemoteIP); break; } NetServer.Clients.Add(new NetClient.Info(cn, n.RemoteIP)); NetData cw = new NetData(NetMessageType.ConnectPass); NetUdp.Send(cw, n.RemoteIP); } break; case NetMessageType.ConnectPass: NetClient.Connect(n.RemoteIP); break; default: Debug.Log("未知数据包,来自:" + n.RemoteIP); break; } n.Reset(); if (NetDataEvent != null) { NetDataEvent(n); } NetDatas.RemoveAt(0); } catch (Exception e) { Debug.LogError("数据包解析错误:" + e.Message.ToString()); NetDatas.RemoveAt(0); } } }