Search Unity

Third Party Photon doesn't disconnect from server

Discussion in 'Multiplayer' started by MaciejKusztal, Jun 26, 2021.

?

How to disconnect from the server and connect for the second game?

  1. Disconnect, connect and start another game.

    10 vote(s)
    76.9%
  2. Disconnect, connect and start another game.

    5 vote(s)
    38.5%
Multiple votes are allowed.
  1. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    Hi,
    I use Photon (PUN2) in my game. I game all system and its works (when you play the first game). The user connects to a server, lobby etc. Game, after the game user, should disconnect from the server and when wants play again just connect again to the server. Problem because the user can't disconnect. Why?
    First code, when Player finish the game.
    Code (CSharp):
    1. public void EndMatch()
    2.     {
    3.         PhotonNetwork.LeaveRoom();
    4.         PhotonNetwork.LeaveLobby();
    5.         PhotonNetwork.Disconnect();
    6.     }
    The second one, when the user wants to leave the lobby:
    Code (CSharp):
    1. public void OnClickLeaveLobby()
    2.     {
    3.         PhotonNetwork.LeaveRoom();
    4.         PhotonNetwork.LeaveLobby();
    5.         PhotonNetwork.Disconnect();
    6.         Debug.Log("User left from Lobby");
    7.         EnergySystem();
    8.         loadSceneScript.LoadMainMenu();
    9.     }
    After both situation I got the same error "ConnectUsingSettings() failed. Can only connect while in state 'Disconnected'. Current state: Connected"
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    Don't do everything in one method.
    Actually, you can simplify matters: Just disconnect. The server will know you have to leave the room or lobby, etc.
     
  3. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    I use this function when the user is in the Lobby and join or create the room. The user has possible to leave the room.
    When he/she presses the button
    "User left from Lobby"
    "Disconnect from the server"
    " ConnectUsingSettings() failed. Can only connect while in state 'Disconnected'. Current state: Connected".

    Code (CSharp):
    1. public void OnClickLeaveLobby()
    2.     {
    3.         PhotonNetwork.Disconnect();
    4.         Debug.Log("User left from Lobby");
    5.         EnergySystem();
    6.         loadSceneScript.LoadMainMenu();
    7.     }
    Code (CSharp):
    1.  public override void OnDisconnected(DisconnectCause cause)
    2.     {
    3.         base.OnDisconnected(cause);
    4.         Debug.Log("Disconnect from the server");
    5.     }
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    Which version of PUN is that?
     
  5. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    It's my error:

    ConnectUsingSettings() failed. Can only connect while in state 'Disconnected'. Current state: Connected
    #0 GetStacktrace(int)
    #1 DebugStringToFile(DebugStringToFileData const&)
    #2 DebugLogHandler_CUSTOM_Internal_Log(LogType, LogOption, ScriptingBackendNativeStringPtrOpaque*, ScriptingBackendNativeObjectPtrOpaque*)
    #3 (Mono JIT Code) (wrapper managed-to-native) UnityEngine.DebugLogHandler:Internal_Log (UnityEngine.LogType,UnityEngine.LogOption,string,UnityEngine.Object)
    #4 mono_jit_runtime_invoke
    #5 do_runtime_invoke
    #6 mono_runtime_invoke
    #7 scripting_method_invoke(ScriptingMethodPtr, ScriptingObjectPtr, ScriptingArguments&, ScriptingExceptionPtr*, bool)
    #8 ScriptingInvocation::Invoke(ScriptingExceptionPtr*, bool)
    #9 MonoBehaviour::CallMethodIfAvailable(int)
    #10 MonoBehaviour::CallUpdateMethod(int)
    #11 void BaseBehaviourManager::CommonUpdate<BehaviourManager>()
    #12 BehaviourManager::Update()
    #13 InitPlayerLoopCallbacks()::UpdateScriptRunBehaviourUpdateRegistrator::Forward()
    #14 ExecutePlayerLoop(NativePlayerLoopSystem*)
    #15 ExecutePlayerLoop(NativePlayerLoopSystem*)
    #16 PlayerLoop()
    #17 PlayerLoopController::UpdateScene(bool)
    #18 PlayerLoopController::UpdateSceneIfNeeded()
    #19 Application::TickTimer()
    #20 -[EditorApplication TickTimer]
    #21 __NSFireTimer
    #22 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__
    #23 __CFRunLoopDoTimer
    #24 __CFRunLoopDoTimers
    #25 __CFRunLoopRun
    #26 CFRunLoopRunSpecific
    #27 RunCurrentEventLoopInMode
    #28 ReceiveNextEventCommon
    #29 _BlockUntilNextEventMatchingListInModeWithFilter
    #30 _DPSNextEvent
    #31 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:]
    #32 -[EditorApplication checkForFocusChange:]
    #33 __NSFireTimer
    #34 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__
    #35 __CFRunLoopDoTimer
    #36 __CFRunLoopDoTimers
    #37 __CFRunLoopRun
    #38 CFRunLoopRunSpecific
    #39 RunCurrentEventLoopInMode
    #40 ReceiveNextEventCommon
    #41 _BlockUntilNextEventMatchingListInModeWithFilter
    #42 _DPSNextEvent
    #43 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:]
    #44 -[NSApplication run]
    #45 NSApplicationMain
    #46 EditorMain(int, char const**)
    #47 main
    #48 start
    #49 ???

    I don't know what it's wrong because the user connecting with the server.
     
    Last edited: Jun 30, 2021
  6. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    I did a small break after disconnect (5 seconds) and before the change of the scene, but it doesn't help. When I would like to start the next game I can't connect with the server and go to the lobby.
     
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,072
    Are you using PUN 2.33.3? If not, update and report if the problem is solved or not.
    If not, we'd need some repro steps, I guess.
     
  8. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    It's okay. I solved it.
     
    tobiass likes this.
  9. DPretolesi

    DPretolesi

    Joined:
    Jun 10, 2021
    Posts:
    1
    Hi! Running the same issue here, could you share your solution?
     
  10. MaciejKusztal

    MaciejKusztal

    Joined:
    Feb 4, 2020
    Posts:
    87
    I'm sorry but It was such a time ago I don't remember what I did.