For the complete documentation index, see llms.txt. This page is also available as Markdown.

Authentication

Summary

This tutorial demonstrates how to send authentication arguments to the server when you connect, how an OnAuthenticate handler can be registered on the server to authenticate connecting players, and how to send arbitrary data back to the client in the server's authentication response. In this tutorial a random number is sent to players when they pass authentication, and an error message is sent when they fail. Clients will pass or fail authentication depending on which button they press to connect.

Requirements

Setup

Create a Room

  1. Right click in the hierarachy window and create a 'Reactor/Room' game object.

  2. Uncheck the 'Connect On Start' checkbox in the ksConnect inspector. This will prevent the script from connecting automatically when the scene is loaded. We will instead create a script to connect when a button is pressed.

Scripting

First we will create a client script to connect when a button is pressed, and handle authentication respones. Then we will create a* server room script to handle player authentication.

Create a ConnectHandler Monobehaviour

This script has event handlers for the ksConnect component's OnGetRooms and OnConnect events. It will attempt to connect to the server when the users presses one of the '0'-'2' keys, and disconnect when 'd' is pressed. The OnGetRooms event allows developers to control which room the client connects to and pass optional authentication arguments to the server. This version of the script connects to the first room available and passes a different argument depending on which number key was pressed. The OnConnect event is fired when a connection attempt completes, even if the connection was not successful, allowing you to handle connection errors. This script logs value of the first optional ksMultiType the server authentication handler returned in ksAuthenticationResult.Data if the connection was succesful, and otherwise logs a connection failure warning depending on the ksAuthenticationResult.Code returned by the authentication handler.

Just like RPCs, the Connect method can take any number of optional ksMultiType arguments. These arguments are passed to any Room.OnAuthenticate handler functions in server room scripts.

  1. Create a new MonoBehaviour named 'ConnectHandler'.

  2. Attach it to the 'Room' game object.

  3. Edit the file to match the code below.

  4. Select the ksConnect component on the room and add the 'ConnectHandler' 'OnGetRooms' method to the ksConnect 'OnGetRooms' event list.

    • Click the '+' button in ksConnect 'OnGetRooms' event list.

    • Drag the Room instance into the new object field.

    • Select 'ConnectHandler->OnGetRooms' in the function selector.

  5. Add the 'ConnectHandler' 'OnConnect' method to the ksCOnnect 'OnConnect' event list.

ConnectHandler.cs

Create a Server Authentication Room Script

The server room script registers a Room.OnAuhthenticate handler that passes authentication if it was called with a single argument with the value 1. It sends a random number in the ksAuthenticationResult.Data to players who pass authentication. If the wrong number of arugments were sent, it fails authentication with ksAuthenticationResult.Code 1, and if any argument value other than 1 was sent, it fails with ksAuthenticationResult.Code 2 and sends an error message in the ksAuthenticationResult.Data.

Authentication handlers are async methods that return a Task, so you can await external asynchronous authentication calls. For more information, see here. Authentication handlers cannot be added or removed after the room is loaded and Initialize is called on the initial scripts.

The ksAsyncResult constructor takes a uint code parameter and an optional number of ksMultiTypes. Authentication fails if the uint code is non-zero. The ksAsyncResult is sent to the client and can be accessed in the Room.OnConnect event. The code and ksMultiTypes are logged by the ksConnect script when authentication fails. If there are multiple authentication handler functions and authentication fails, the ksAsyncResult sent to the client will be from the first authentication function that failed. If all authentication functions pass, the ksMultiType arguments from all authentication functions are combined into one array and sent to the client.

It is possible that OnPlayerJoin and OnPlayerLeave events may not fire for a player who passes authentication if they disconnect during the authentication process. You should not set any persistent state in an authentication handler such as user-login state tracking and rely on 'OnPlayerLeave' to clear that state.

  1. Select the 'Room' object.

  2. In the 'Add Component' menu, select 'Reactor->New Server Room Script'.

  3. Name the script 'ServerAuthenticationRoom'.

ServerAuthenticationRoom.cs

Testing

  1. Build your scene config (CTRL + F2).

  2. Start a local server.

  3. Enter play mode.

  4. Press 1. You should see a message in the Unity logs "Connected. Server sent result value X" where X is a random number.

  5. Press 2. You should get a ROOM_INITIALIZE_ERROR connection failure warning. This happens when you try to connect to the same room twice.

  6. Press D to disconnect.

  7. Press 2. You should get a connection failure warning with "AUTH_ERR_USER_DEFINED (code: 2, data: [Unknown auth arg: 2])"`.

  8. Press 0. You should get a warning "Authentication failed: wrong number of authentication arguments."

Last updated