public override void OnCalled(Message msg, NetIncomingMessage netMsg = null) { MessageRemovePlayer message = msg as MessageRemovePlayer; var player = MultiplayerManager.instance.m_players.FirstOrDefault(x => x.ID == message.playerId); if (player != null) { GameObject.Destroy(player.PlayerGameObject); MultiplayerManager.instance.m_players.Remove(player); } }
void Update() { NetIncomingMessage msg; while (m_server != null && (msg = m_server.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: Debug.Log(msg.ReadString()); break; case NetIncomingMessageType.WarningMessage: Debug.LogWarning(msg.ReadString()); break; case NetIncomingMessageType.ErrorMessage: Debug.LogError(msg.ReadString()); break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); string reason = msg.ReadString(); Debug.Log(NetUtility.ToHexString(msg.SenderConnection.RemoteUniqueIdentifier) + " " + status + ": " + reason); if (status == NetConnectionStatus.Connected) { Debug.Log("Remote connected: " + msg.SenderConnection.RemoteHailMessage.ReadString()); NetOutgoingMessage om = m_server.CreateMessage(); om.Write((int)MessageFunction.SpawnAllPlayer); om.Write(m_players.Count); foreach (var player in m_players) { om.Write(player.ID); om.Write(player.Name); } m_server.SendMessage(om, msg.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0); } else if (status == NetConnectionStatus.Disconnected) { MultiplayerManager mInstance = MultiplayerManager.instance; var player = m_players.FirstOrDefault(x => x.OwnerConnection == msg.SenderConnection); if (player != null) { MessageRemovePlayer message = new MessageRemovePlayer(); message.playerId = player.ID; mInstance.SendNetworkMessage(message, SendTo.All); } } break; case NetIncomingMessageType.Data: HandleSeverMessage(msg); break; case NetIncomingMessageType.UnconnectedData: var msgType = (UnConnectedMessageFunction)msg.ReadInt32(); switch (msgType) { case UnConnectedMessageFunction.PingServerForPlayerCount: NetOutgoingMessage returnMessage = m_server.CreateMessage(); returnMessage.Write((int)UnConnectedMessageFunction.PingServerForPlayerCount); returnMessage.Write(m_players.Count); m_server.SendUnconnectedMessage(returnMessage, msg.SenderEndPoint); break; case UnConnectedMessageFunction.PingServerForMapId: NetOutgoingMessage returnMessage2 = m_server.CreateMessage(); returnMessage2.Write((int)UnConnectedMessageFunction.PingServerForMapId); returnMessage2.Write(m_players.Count); returnMessage2.Write(m_saveWorkshopID); m_server.SendUnconnectedMessage(returnMessage2, msg.SenderEndPoint); break; } break; default: Debug.LogWarning("Unhandled type: " + msg.MessageType); break; } m_server.Recycle(msg); } while (m_client != null && (msg = m_client.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Debug.Log(msg.ReadString()); break; case NetIncomingMessageType.StatusChanged: if (msg.ReadString() == "=Failed".Trim()) { Debug.Log("It failed"); m_failedToConnect = true; } break; case NetIncomingMessageType.Data: HandleClientMessage(msg); break; default: Debug.LogWarning("Unhandled type: " + msg.MessageType); break; } m_client.Recycle(msg); } }