category: Application fundamentals

When does it happen?

Disconnection from scenes happen in different cases:

  • If a client calls the Disconnect method of the Scene object.
  • If the client loses connection to the server (Stormancer will automatically try to reconnect him, but will consider the client disconnected after some time).
  • If the client closes the application
    • If the server call Disconnect on the IScenePeerClient object representing the client.

How to handle it?

On the server, add a handler to the OnDisconnect event when creating your scene template. The event is fired whenever an user is disconnected from the scene node.

scene.OnDisconnected.Add(async peer =>
        //Cleanup resources here.

Handling unexpected client disconnections

If your client loses the connection, it will try automatically to reconnect. However, if it is unsuccessful in doing so, your client's state will become disconnected.

When entering the Disconnected state, Stormancer tells you that it isn't able anymore to recover from the disconnection. On the server, the OnDisconnected event will be fired with the "Connection lost" reason. On the client, scenes objects become invalid.

To reconnect, create a new client object and perform all the logic required by your game.