#Event for session terminated because of host left

1 messages · Page 1 of 1 (latest)

ashen basin
#

i did try the

NetworkManager.Singleton.OnServerStopped += b => StartCoroutine(Restart());

    private IEnumerator Restart()
    {
        Debug.Log("marked");
        yield return new WaitForEndOfFrame(); 
        StartHost().Forget();
    }

but somehow it didnt respond when host left the session -> session terminated , not even the debug log

is there any more specfic events that can let me react to this? i need to respond to this situation so i can create a new session for players to get back

tepid solstice
ashen basin
#

i think i have only two choice now

  1. disconnect client using networkmanager instead of unity session

  2. send msg to players using push messages from cloud code

#

which one is better?

tepid solstice
#

Probably 1

#

It also seems like with the level of organization and manipulation of sessions that you're wanting, you may be better off just using the Lobby Package directly as it doesn't auto connect you with NetworkManager.

ashen basin
# tepid solstice It also seems like with the level of organization and manipulation of sessions t...

for me these must be achieved:

  1. able to query a list of active sessions automatically or manually
  2. able to set privacy level based on some parameters or properties
  3. when host left , session terminated, and all players create/return to their own session
  4. leave previous session after joined new one successfully, if something bad happened on joining new one, then give up, just stay in the old session
  5. the architecture should be able to port to DGS without rewriting the whole base logic
tepid solstice
#

Sessions is just a wrapper the uses Lobby/Relay/DA/NGO under the hood. Lobby can do all of those things and potentially more as Unity is still working to make Sessions provide what Lobby provides.

#

I think a Unity dev mentioned that Sessions will support not auto joining w/ NetworkManager at some point, but no idea when that would be released.

ashen basin
#

i think i will take a look at lobby then...

tepid solstice
#

I think Sessions in its current state is great if you have a very simple setup where you just want users to open your game, be able to browse Sessions, join one of them and just be auto connected to the game.

ashen basin
#

like it needs more

tepid solstice
#

Being able to have a Lobby without auto connecting to an NGO session is nice because you can represent a Lobby purely as UI without being connected to an actual game session.

#

Which is how most games handle things

ashen basin
#

yeah actually when player logined, i will make them always starts with "single player" , but actually they did created/joined a session already, its just their session are locked and hidden

ashen basin
tepid solstice
#

You can use one on their own, even both at the same time. Whatever your game requires.

#

Generally speaking you'd only need one or the other.

ashen basin
#

yeah i think so

#

because if lobby can do what i required, i just gonna use that instead

ashen basin
tepid solstice
#

There is zero forced integration with the NetworkManager. You can create a Lobby and it exists as its own entity without being attached to anything else.

ashen basin
#

nice

#

i gonna spend the rest of this week to check lobby out , ty 👍

ashen basin
tepid solstice
ashen basin
#

ohhhhh

tepid solstice
#

You'd still be pairing up something like Relay to take that group of players and connect them to an NGO session.

ashen basin
#

ok i think i will still setup relay

civic dagger
#

You can use sessions without a network it acts just like a Lobby

ashen basin
#

hi

#

i think right now if i can take care of player responses, i can still stick to session

ashen basin
# civic dagger You can use sessions without a network it acts just like a Lobby

so i think at the moment theres one last problem apart from the index one , but i think its crucial to deal with

when a host left the session, the client joined ofc will be leaving as well, i was wondering if theres a specfic event that aims on "host left, session terminated" , but not regular disconnecting from session like being kicked or leaving the room normally

#

i tried stateChange, but it cannot capture when host left

tepid solstice
#

Deleted, Disconnected and None should cover all possible ways of being booted from a session. You may just be experiencing a timeout period where the Session has not shut down yet.

civic dagger
#

You might get a Connection Event before the session event? but definitely both should fire off

ashen basin
#

i think i need to show the whole flow of my session handling first

#
            NetworkManager.Singleton.Shutdown();
            
            ISession newSession = await MultiplayerService.Instance.CreateSessionAsync(hostOption);
            
            UIManager.Instance.CloseAllUI();
            
            await LeaveSession(SessionConstants.SessionOwnership.Host);
            CurrentSession = newSession;
            CurrentSession.SetPrivacyState(privacy, password);
            CurrentSession.StateChanged += state =>
            {
                switch (state)
                {
                    case SessionState.None:
                        Debug.Log("none");
                        break;
                    case SessionState.Connected:
                        Debug.Log("connected");
                        break;
                    case SessionState.Disconnected:
                        Debug.Log("disconnected");
                        break;
                    case SessionState.Deleted:
                        Debug.Log("deleted");
                        break;
                }
            };```
#

this is start host

#
            NetworkManager.Singleton.Shutdown();

            ISession newSession = await MultiplayerService.Instance.JoinSessionByIdAsync(sessionID,joinPassword);
            
            UIManager.Instance.CloseAllUI();
            
            await LeaveSession(SessionConstants.SessionOwnership.Client);
            CurrentSession = newSession;
            CurrentSession.StateChanged += state =>
            {
                switch (state)
                {
                    case SessionState.None:
                        Debug.Log("none");
                        break;
                    case SessionState.Connected:
                        Debug.Log("connected");
                        break;
                    case SessionState.Disconnected:
                        Debug.Log("disconnected");
                        break;
                    case SessionState.Deleted:
                        Debug.Log("deleted");
                        break;
                }
            };```
#

this is start client

#
    private async UniTask LeaveSession(SessionConstants.SessionOwnership ownership)
    {
        CommunicationManager.Instance.isJoinRequestRestricted = true;

        if (CurrentSession == null)
        {
            return;
        }
        
        if (CurrentSession.Host == AuthenticationService.Instance.PlayerId)
        {
            await CurrentSession.AsHost().LeaveAsync();
        }
        else
        {
            await CurrentSession.LeaveAsync();
        }

        switch (ownership)
        {
            case SessionConstants.SessionOwnership.Host:
                NetworkManager.Singleton.StartHost();
                break;
            case SessionConstants.SessionOwnership.Client:
                NetworkManager.Singleton.StartClient();
                break;
        }
    }```
#

this is leave session

#

so basically the workflow is

  1. shutdown network manager to be ready for ownership change
  2. create or join a session
  3. if its successful, leave previous session and kickstart network manager again
  4. the new session is the current session

(test)5. register stateChange event

#

so im replicating player A joined player B, and then player B joined player C, by then player A should be disconnected, because host left, session terminated

#

currentSession can only be set with these functions

ashen basin
#

i had registered stateChange to session and OnConnectionEvent

NetworkManager.Singleton.OnConnectionEvent += (manager, data) =>
{
    Debug.Log(" c marked >>> " + data.EventType);
};```
#

this is the only event that came back

#

at least if stateChange doesnt work, on connection event can

civic dagger
#

I think disconnecting might remove the local session before the session events fire

ashen basin
#

then connection event is more stable

#

btw not related to this topic tho, will there be further imvestigation and even patches in future about the "index error" we saw earlier?

civic dagger
ashen basin
#

ty👍

i think tmr i will just focus on figuring out these bugs

ashen basin
#

i had tested with some use case and confirmed that in the case of "host left, session terminated, client left either"
on connection event will trigger faster than session event on client side

#

and disconnection will happen faster than session events like statechanged

#

i really dont want to send messages using cloud code and fire up and receiver event for clients before theyre disconnected, thats just seem primitive and inefficient, and it occupies traffic too, this will be my last resort if i cant find any solutions

so i will register a special onconnection event

ashen basin
#
    private void test(NetworkManager manager , ConnectionEventData data)
    {
        if (data.EventType == ConnectionEvent.ClientDisconnected)
        {
            Debug.Log("session terminated or being kicked");
        }
    }```

```cs
NetworkManager.Singleton.OnConnectionEvent -= test; 

try
{
    NetworkManager.Singleton.Shutdown();

    ISession newSession = await MultiplayerService.Instance.JoinSessionByIdAsync(sessionID,joinPassword);

    UIManager.Instance.CloseAllUI();
    
    await LeaveSession(SessionConstants.SessionOwnership.Client);
    CurrentSession = newSession;
}

NetworkManager.Singleton.OnConnectionEvent += test;```

when player connected inside the session and initiated, register the event, and then unregister when player voluntarily joins other room/return to other room
#

apart from player choose to join other room or being kicked, there shouldnt be anymore normal cases/reasons that can cause player to disconnect?

civic dagger
ashen basin
#

heres my final decision:

private async UniTask LeaveSession(SessionConstants.SessionOwnership ownership)
{
    CommunicationManager.Instance.isJoinRequestRestricted = true;

    if (CurrentSession == null)
    {
        return;
    }
    
    if (CurrentSession.Host == AuthenticationService.Instance.PlayerId)
    {
        foreach (IReadOnlyPlayer element in CurrentSession.Players)
        {
            if (element.Id == AuthenticationService.Instance.PlayerId)
            {
                continue;
            }

            await CommunicationManager.Instance.SendMsgToPlayer("host left, session terminated\n returning to own session.", CommunicationConstants.MessageType.SessionTerminated, element.Id);
        }
        
        await CurrentSession.AsHost().LeaveAsync();
    }
    else
    {
        await CurrentSession.LeaveAsync();
    }

    switch (ownership)
    {
        case SessionConstants.SessionOwnership.Host:
            NetworkManager.Singleton.StartHost();
            break;
        case SessionConstants.SessionOwnership.Client:
            NetworkManager.Singleton.StartClient();
            break;
    }
}```
#

before starting a host session or join as a client , the first thing player do is to shutdown network manager

#

and then leave room using above function

#

if player leaving is a host too, he will send messages using cloud code

#

this is primitive, but it is the most stable way as u can make sure everyone will receive leave messages before host left

#

and after i tested it, it works well