Connecting to the Lobby System

The first steps in starting a uLobby session are to initialize the lobby server and then connect game servers and game clients to it.

Initializing the Lobby Server

The lobby server is initialized using the Lobby.InitializeLobby() method. This method takes as argument the maximum number of connections the lobby server accepts, and the port to listen to for incoming connections. If the lobby server was successfully initialized, the Lobby.OnLobbyInitialized event will be raised. After this, game servers and clients are ready to connect to the lobby server. Otherwise, if anything went wrong, InitializeLobby() will return an error informing of what went wrong.

Connecting a Game Server or Client

The Lobby.ConnectAsServer() and Lobby.ConnectAsClient() methods are used for connecting game servers and clients, respectively. They take as argument the host IP address and port of the lobby server to connect to. By choosing to connect as either a game server or a client, the lobby system knows what type of peer you represent and will treat you as such for the rest of the session.

If the connection is successful, the Lobby.OnConnected event will be raised. This callback is the same for both game servers and clients. You can use the Lobby.peerType property to find out your peer type. This will be set to one of Lobby, Server or Client when you are connected, or to either Connecting or Disconnected when attempting to connect or not connected, respectively. You can also use the Lobby.isLobby, Lobby.isServer and Lobby.isClient properties to quickly determine your peer type.

You can use the Lobby.connectionStatus property if you are only interested in finding out the connection status, regardless of what peer type you have. This property will have one of the values Connecting, Connected, Disconnecting or Disconnected. The Connecting status will only result while connecting to the lobby server as a game server or client. The Disconnecting status will only result after disconnecting explicitly from the lobby system, in the peer that initiated the disconnection.

Peers

Each game server and client, as well as the lobby server itself, is identified using the LobbyPeer class. This class represents a unique peer in the network for the current session. The Lobby.peer property represents your own peer, and the Lobby.lobby property the lobby server's peer (in the lobby server, these are one and the same). LobbyPeer instances are only valid for the duration of the session of that peer, so if a host disconnects and reconnects again it will get a new peer instance.