public void Initialize(GameRoomMessagingHub messagingHub, NetworkServer networkServer, GameLiftServer gameLiftServer) { if (messagingHub == null || networkServer == null || gameLiftServer == null) { UnityEngine.Debug.LogError("GameRoomContext can not be initialized."); return; } _MessagingHub = messagingHub; _NetworkServer = networkServer; _GameLiftServer = gameLiftServer; _MessagingHub.OnReceivedSystemUserIdAsObservable() .Subscribe(networkClientUser => { bool contained = _NetworkClientUsers.ContainsKey(networkClientUser.UserId); bool accepted = true; if (!_IsLocalServer) { accepted = _GameLiftServer.AcceptPlayerSession(networkClientUser.UserId); } if (!contained && accepted) { _NetworkClientUsers.Add(networkClientUser.UserId, networkClientUser); } else { _NetworkServer.DisconnectClient(networkClientUser.ClientId, "Cannot accept system user id"); } }) .AddTo(this); _MessagingHub.OnReceivedPlayerEjectionAsObservable() .Subscribe(player => { string userId = player.UserId; if (_NetworkClientUsers.TryGetValue(userId, out NetworkClientUser networkClientUser)) { _NetworkClientUsers.Remove(userId); _GameLiftServer.RemovePlayerSession(networkClientUser.UserId); _NetworkServer.DisconnectClient(networkClientUser.ClientId, "PlayerEjection"); } }) .AddTo(this); _NetworkServer.OnClientDisconnectedAsObservable() .Subscribe(clientId => { var networkClientUser = _NetworkClientUsers.Select(kv => kv.Value).FirstOrDefault(v => v.ClientId == clientId); if (networkClientUser != null) { _NetworkClientUsers.Remove(networkClientUser.UserId); _GameLiftServer.RemovePlayerSession(networkClientUser.UserId); } }) .AddTo(this); }
public override void KickClient(string reason) { string result = ""; result = $"\n\nMESSAGE FROM SERVER:\n\nYou have been disconnected for the following reason(s): {reason}\n\n"; result += "Thank you for your support! Please come back and try again!\n\n"; Console.WriteLine(result); NetworkServer.DisconnectClient(this, result, reason); }