Connecting Lobby, Servers and Clients
Connecting Lobby, Servers and Clients
The first steps in starting a uLobby session are to initialize the lobby and
then connect game servers and game clients to it.
Initializing the Lobby
The lobby is initialized using the
Lobby.InitializeLobby() method. This
method takes as argument the maximum number of connections the lobby accepts,
and the port to listen to for incoming connections. If the lobby was
successfully initialized, the
Lobby.OnLobbyInitialized event will be
raised. After this, game servers and clients are ready to connect to the lobby.
Otherwise, if anything went wrong,
InitializeLobby() will
return an error informing of what went wrong.
Connecting a Server or a 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 to connect to. By choosing to
connect as either a server or a client, the lobby 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.
Peers
Each server and client, as well as the lobby 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's peer (in the lobby,
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.