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 bool HandleConnect(int connectionId, string playerSessionId) { Debug.Log("HandleConnect"); var outcome = _gameLiftServer.AcceptPlayerSession(playerSessionId); if (outcome.Success) { Debug.Log("PLAYER SESSION VALIDATED"); } else { Debug.Log("PLAYER SESSION REJECTED. AcceptPlayerSession() returned " + outcome.Error.ToString()); } // track our player sessions _playerSessions.Add(connectionId, playerSessionId); return(outcome.Success); }