Connection Events

Callbacks on connection methods

Strix uses an asynchronous, event-based method for making connections. Calls to connection functions return immediately after a request is made, and Strix listens asynchronously for a success or failure message.

The connection functions (and many other functions) take event handlers. These handlers will be called on success or failure of the connection request.

Note

Although we call them events, the success and failure callbacks are ordinary C# delegates passed through method arguments.

Events for subscription

In addition to success and failure callbacks, Strix provides several events that you can subscribe.

For connections, there are 4 specific events:

Event

Handler type

Available properties in arguments

Connected

StrixNetworkConnectEventHandler

session: The new session.

ConnectFailed

StrixNetworkConnectFailedEventHandler

session: The session we failed to reconnect to (if applicable).
cause: The cause of the failure.

Closed

StrixNetworkCloseEventHander

session: The session that was closed.

ErrorThrown

StrixNetworkErrorEventHandler

session: The session the error occurred in.
cause: The cause of the error.

These four events are C# events, meaning you can use += operator (or add accessor) to subscribe and -= operator (or delete accessor) to unsubscribe. They are all defined on SoftGear.Strix.Unity.Runtime.Session.AbstractSession abstract class, and you will usually use them via masterSession and roomSession properties of StrixNetwork singleton.

See Session events for details of these events.

Examples

Here is an example of how connection events can be used.

public void ConnectedEventSample(string applicationId, string host, string playerName)
{
    StrixNetwork.instance.roomSession.Connected += args => {
        Debug.Log("Room session connection has been established with the following host: "
            + args.session.host
            + ":" + args.session.port
        );
    };

    StrixNetwork.instance.applicationId = applicationId;
    StrixNetwork.instance.ConnectMasterServer(
        host: host,
        connectEventHandler: _ => {
            StrixNetwork.instance.JoinRandomRoom(
                playerName,
                handler: joinRoomResult => { },
                failureHandler: joinRoomError => {
                    StrixNetwork.instance.CreateRoom(
                        new RoomProperties {
                            name = "New Room",
                            capacity = 4,
                        },
                        new RoomMemberProperties {
                            name = playerName
                        },
                        createRoomResult => { },
                        createRoomError => Debug.LogError(
                            "Could not join nor create a room."
                            + " Reasons: " + joinRoomError.cause
                            + "; " + createRoomError.cause
                        )
                    );
                });
        },
        errorEventHandler: connectError => Debug.LogError("Connection failed. Reason: " + connectError.cause)
    );
}

Error Codes

ConnectFailed and ErrorThrown event handlers receive cause property on their arguments. It is declared as Exception type, but often you can downcast it to ErrorCodeException to get error codes. See the Error Handling section for information about retrieving and understanding specific error codes.