// Update is called once per frame void Update() { if (hostID <= EMPTY_HOST_ID) { if (Input.GetKeyDown(KeyCode.C)) { NetworkTransport.Init(); ConnectionConfig connConfig = new ConnectionConfig(); reliableChannelID = connConfig.AddChannel(QosType.Reliable); unreliableChannelID = connConfig.AddChannel(QosType.Unreliable); HostTopology hostTopology = new HostTopology(connConfig, 1); hostID = NetworkTransport.AddHost(hostTopology); connectionID = NetworkTransport.Connect(hostID, serverIP, serverPort, 0, out error); recBuffer = new byte[recBufferSize]; waitingJoin = true; } } else { NetworkEventType eventType = NetworkTransport.ReceiveFromHost(hostID, out recConnectionID, out channelID, recBuffer, recBufferSize, out receivedSize, out error); switch (eventType) { case NetworkEventType.ConnectEvent: Debug.Log("成功连接到服务器"); break; case NetworkEventType.DataEvent: Debug.Log("接收到服务器的新数据"); ExecuteInstruction(NetworkCommunicateHelper.UnPack(recBuffer)); break; case NetworkEventType.DisconnectEvent: Debug.Log("有客户端断开连接"); break; case NetworkEventType.Nothing: Debug.Log("无数据"); break; default: break; } SendAxis("H"); SendAxis("V"); SendInputDirection(); SendKey(KeyCode.Mouse0); SendKey(KeyCode.Mouse1); SendKey(KeyCode.LeftShift); SendKey(KeyCode.Space); SendKey(KeyCode.B); } }
// Update is called once per frame void Update() { if (hostID <= EMPTY_HOST_ID) { if (Input.GetKeyDown(KeyCode.F1)) { isServer = true; } if (Input.GetKeyDown(KeyCode.F2)) { isServer = false; } if (Input.GetKeyDown(KeyCode.C)) { if (isServer) { NetworkTransport.Init(); ConnectionConfig connConfig = new ConnectionConfig(); reliableChannelID = connConfig.AddChannel(QosType.Reliable); unreliableChannelID = connConfig.AddChannel(QosType.Unreliable); HostTopology hostTopology = new HostTopology(connConfig, maxPlayerCount); hostID = NetworkTransport.AddHost(hostTopology, serverPort); recBuffer = new byte[recBufferSize]; } else { enabled = false; } } } else { NetworkEventType eventType = NetworkTransport.ReceiveFromHost(hostID, out recConnectionID, out channelID, recBuffer, recBufferSize, out receivedSize, out error); switch (eventType) { case NetworkEventType.ConnectEvent: int netCode = EMPTY_NETCODE; if (TryPortionNetCode(recConnectionID, out netCode)) { currentConnCount++; // 通知新玩家设置自己的编号 sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, "SetPlayerNo", netCode); NetworkTransport.Send(hostID, recConnectionID, unreliableChannelID, sendBuffer, sendBufferSize, out error); // 通知所有玩家设置各信息 List <object> synPlayerInfoDataList = new List <object>(); synPlayerInfoDataList.Add("SynchronizePlayerInfo"); for (int i = 0; i < currentConnCount; i++) { synPlayerInfoDataList.Add(netCodeList[i]); } sendBuffer = NetworkCommunicateHelper.ToPack(out sendBufferSize, synPlayerInfoDataList.ToArray()); SendToEveryPlayer(hostID, unreliableChannelID, sendBuffer, sendBufferSize, out error); //// 通知所有玩家同步位置 //SynchronizeAllPlayerTransform(); Debug.Log("有新客户端连接,当前连接数 " + currentConnCount + "/" + maxPlayerCount); } break; case NetworkEventType.DataEvent: Debug.Log("接收到客户端的新数据"); ExecuteInstruction(recConnectionID, NetworkCommunicateHelper.UnPack(recBuffer)); break; case NetworkEventType.DisconnectEvent: int recyclePlayerNo = EMPTY_NETCODE; TryRecycleNetCode(recConnectionID, out recyclePlayerNo); Debug.Log("有客户端断开连接,当前连接数 " + currentConnCount + "/" + maxPlayerCount); break; case NetworkEventType.Nothing: Debug.Log("无数据"); break; default: break; } //if (Time.time - lastSynchronizeTime > synchronizeFrequency) //{ // // 通知所有玩家同步位置 // SynchronizeAllPlayerTransform(); // lastSynchronizeTime = Time.time; //} } }