How do I disconnect from a Dedicated Server the right way?

Simple question I feel, but right now the only way I can do this successfully is to attempt client travel that cannot occur, which forcibly disconnects the client from the server, I then destroy the session. I have tried the Logout node and the Destroy Session node by themselves and in unison, however it fails to send me back to the login screen, and the player controller can no longer interact with any online blueprint nodes. I would expect there to be a single command that disconnected the client from the server and then left the rest of the decisions up to the game developer.
Hope someone has figured this out.
Spiris

I’m looking for a way to do this as well.

I hate to say it, but this is still a problem. In this situation is there a call required on the server first to inform the client they are being disconnected? The destroy session node sounds like a terrible idea on the server, but perhaps the ‘logoff’ node will work. This is currently not clear, however, and documentation references or someone with the knowledge would be appreciated.

This thread has been a long time running, apparently without any advice. Not to mention that I am seeing updates about comments that aren’t even visible on it. Is this something Unreal Engine simply cannot do? It seems like a huge viability concern for anyone making an online game…

If you use sessions nodes, there is a “logout” node to disconnects a player, you just need his controller !!!

Hello again everyone,
I did a large amount of digging, but I stumbled upon a workaround. First to answer my question:

You cannot disconnect from a dedicated server correctly. But that is ok.

It is currently not supported by the engine. Whatever the case, mistakes were made and ignored. Moving on. Now for the workaround. With tips and tricks for things that aren't terribly transparent.

My Workaround

To Disconnect Intentionaly: Destroy the Session, then on success or fail, open your login level. This will handle disconnecting you to an extent. To Disconnect on accident: Check to be sure you are receiving a network or travel error on the client, then open your login level.

Now for the magic

You may be in a situation where you cannot log back in. This is where the error was the hardest to identify, but basically the session you would have thought got destroyed earlier in fact still lives. This may be a network error issue, I dont know. So long as that session lives you cannot reconnect to any server. So in your connection logic, simply call DestroySession first. On Success or Fail attempt to join session and you should be clear.

Tricks

1) UE4 has a default engine map called "Entry". If you name your login level "Entry" THAT IS BAD. The engine will get confused at some point and try to reroute you to that level, which is not your login level, and the player has to deal with that. Name your map something else and you will be saved from my headache. . 2) You may need to destroy your player controller on intentional logging out. You may not. This is still a gray zone for me as sometimes it is necessary, and sometimes opening the new level handles correctly. If there is anything else anyone can clarify for this, any info is good info. :) ~Spiris

Just found this. The tip to “destroy session” everytime before creating a new one seems so obvious. Helped me alot! Thanks

This is a bit old, but I was running into this issue fairly recently. I wanted the server to close all the client’s connections once the match was over.

Destroying the PlayerControllers like the accepted answer says to do worked, but it caused all the clients to “panic” because from their perspective they were losing connection. This had the potential to leave either the client or server in a weird state.

The solution was to call GetEngine()->HandleDisconnect(World, World->GetNetDriver()); from the client. I replicated the current match “phase” to the clients on the GameState, and when that phase was modified it checked if it was the “match end” phase. If so, the client called the above function and disconnected itself.

You can look inside the code for UGameInstance for examples; there’s also a ReturnToMainMenu function in the GameState which I didn’t try but seems fairly promising.

3 Likes

There is a some delay to the moment of detecting disconnect event. Maybe i have delay because logs .

I will share the way that I found. In Blueprints, use “Execute Console Command” (Command = “disconnect”, Specific Player = Player Controller which needs to be disconnected from the server). After executing this command, the player automatically opens the level that is specified by default in the project settings.
BTW this raises the “NetworkError” event (Connection Lost and Failure Received) In the player’s GameInstance.

3 Likes

Worked for me, thx bro.

Had the same problems:

My Steps to join / leave ded. server:

  1. Dedicated Server and Client Setup
  2. Client connecst first time to Server:
  3. Then Leaves: by first killing existing Session, then loading different Map in the OnLogout Method in my GameMode
    4.Connect for the second time:

Result:

  • Player could connect first time
    normally, but second time he wasn´t able to be
    spawned correctly.

The Problem:

  • I´ve overriden the Event:
    HandleStartingNewPlayer and my
    Respawning code was flawed.

Taking a look at the Engine Code made my mistake clear. A lot is being done in the GameMode and GameModeBase Classes for you. I tried to impl. very poor spawning logic, which broke the spawning system.

This is the code fromGameModeBase.cpp L: 1017

void AGameModeBase::HandleStartingNewPlayer_Implementation(APlayerController* NewPlayer)
{
	// If players should start as spectators, leave them in the spectator state
	if (!bStartPlayersAsSpectators && !MustSpectate(NewPlayer) && PlayerCanRestart(NewPlayer))
	{
		// Otherwise spawn their pawn immediately
		RestartPlayer(NewPlayer);
	}
}

Depending on what GameMode u use: GameMode or GameModeBase, has it´s own way of spawning players and changeing the game state.
If you override one or more of those methods in your game mode, you need to call the other depending methods accordingly to make the default GameMode Code work.

I did not do that, and my spawning code was flawed. So the spawning didn´t work anymore, after spawning the first client in the map.

NOTE:

  • Check the Engine Code for reference and don´t just overriding methods, like me…Make sure to read the code and get a grip of what the function is supposed to do at a minimum.
  • When customizing the spawning logic you can pretty much just override the methods used in GameMode or GameModeBase from cpp or Blueprint.
  • for disconnecting a player from a dedicated server those two methods work always:

Solutions that will work for dedicated servers:

A. Join / Disconnect Dedicated Server
by IP: (No Sessions)

  • JOIN: open <IPADRESS:PORT> | Node:
    Execute Command or OpenLevel works
  • DISCONNECT: disconnect | Node: Execute Command

B. Join / Disconnect Dedicated Server by Sessions
(Advanced Sessions and regular Online Subsystem from epic will work)

  • JOIN: use Node: Join Session | Needs an Session Result BP Object reference and PlayerController Ref
  • DISCONNECT: use Node: Destroy Session | Needs PlayerController Ref

Seems to work fine for me. can rejoin the server after I leave as well and create new sessions. Only commenting to add to the collection of fixes here for anyone looking in the future.

1 Like

Thank you @Jay2645 ! I’ve been looking for this for few hours.