Skip to main content

Connection events

When you need to react to connection or disconnection events for yourself and other clients, you can use NetworkManager.OnConnectionEvent as a unified source of information about changes to the network topology. OnConnectionEvent will receive a ConnectionEventData struct detailing the relevant information about the connection state change:

public enum ConnectionEvent
{
ClientConnected,
PeerConnected,
ClientDisconnected,
PeerDisconnected
}

public struct ConnectionEventData
{
public ConnectionEvent EventType;
public ulong ClientId;
public NativeArray<ulong> PeerClientIds;
}

There are four types of event you can receive. The events are the same for both clients and servers/hosts, but they indicate slightly different things depending on the context.

EventServer or hostClient
ConnectionEvent.ClientConnectedIndicates that a new client has connected. The ID of the newly connected client is ClientId. PeerClientIds is uninitialized and shouldn't be used.Indicates that the local client has completed its connection to the server. The ID of the client is LocalClientId, and PeerClientIds contains a list of client IDs of other clients currently connected to the server.
ConnectionEvent.PeerConnectedNot applicable for servers. For host clients running in host mode, works the same as for clients.Indicates that another client has connected to the server. The ID of the newly connected client is ClientId. PeerClientIds is uninitialized and shouldn't be used.
ConnectionEvent.ClientDisconnectedIndicates that a client has disconnected. The ID of the disconnected client is ClientId. PeerClientIds is uninitialized and shouldn't be used.Indicates that the local client has disconnected from the server. The ID of the client is LocalClientId. PeerClientIds is uninitialized and shouldn't be used.
ConnectionEvent.PeerDisconnectedNot applicable for servers. For host clients running in host mode, works the same as for clients.Indicates that another client has disconnected from the server. The ID of the disconnected client is ClientId. PeerClientIds is uninitialized and shouldn't be used.