Room Server Connection¶
Room servers are the workhorses of Strix. They contain the information of the different rooms and manage players in them. Room Servers also control and relay messages and events between the players in the room.
A connection to a room server is required to join a room or to use other room features. You can establish a room server connection explicitly (by searching for a room server and calling a connection method) or implicitly (by directly calling a room method).
Note
In most scenarios,
you don’t need to call room-server connection methods explicitly.
Room functions such as CreateRoom
or JoinRoom
automatically connect to an appropriate room server
(if the connection is not made yet).
You also don’t need to disconnect from a room server explicitly. A room server is automatically disconnected when the SDK needs to connect to another room server.
Your client must be connected to the master server (or has been connected before and disconnected due to idle timeout) for this implicit room server connection to work.
Searching for Room Servers¶
In order to connect to a room server explicitly, you need to know its host (IP) address and port number. They can be obtained by performing a search on the master server.
StrixNetwork.instance.SearchRoom(
limit: 10,
offset: 0,
handler: searchResults => {
Debug.Log(searchResults.roomInfoCollection.Count + " rooms found.");
foreach (var roomInfo in searchResults.roomInfoCollection)
Debug.Log("Room ID: " + roomInfo.id
+ "\nHost: " + roomInfo.host
+ "\nPort: " + roomInfo.port
);
},
failureHandler: searchError => Debug.LogError("Search failed. Reason: " + searchError.cause)
);
Note
SearchRoom
method returns a list of rooms,
and you can extract information on room servers from the list as in the above example.
Please be careful on the following points, however:
You may see information of a same room server multiple times in the list because a single room server can host multiple rooms.
You can’t get information on room servers that have no rooms at the moment. Strix Unity SDK provides no easy way to find information on a room server that has no rooms.
Connecting to a Room Server¶
After searching for room server information, you can call the Connect
method on the roomSession
property of the StrixNetwork
singleton.
StrixNetwork.instance.roomSession.Connect(
host: roomInfo.host,
port: roomInfo.port,
protocol: roomInfo.protocol,
connectEventHandler: connectResult => Debug.Log("Successfully connected to the room server."),
errorEventHandler: connectError => Debug.LogError("Connection failed. Reason: " + connectError.cause)
);
As with other SDK methods, you can pass callbacks to be called when the request finishes with either a success or a failure.
If you’re already connected to some room server, calling Connect
will first close the previous connection.
Note
In addition to a host address and a port number, you also need to specify a protocol to connect to a room server as in the above example, because Strix servers support multiple transport protocols. It is a good idea to get the protocol out of RoomInfo and not to hard-code it.
Connecting to a Room Server Implicitly¶
Usually, you do not need to query the master server manually to connect to a room server. Strix provides functions for joining rooms that automatically connect to the Room Server of the room to join.
See JoinRoom or JoinRandomRoom for more information.
Disconnection¶
You can disconnect from a room server when you don’t need to use a room. Disconnection will prevent further synchronization and will result in the client leaving from the room if you are in one.
StrixNetwork.instance.roomSession.Disconnect();
Note
Even after you manually disconnected from a room server,
StrixNetwork
may automatically connect to a same (or different) room server
if you requested an operation that requires a room server connection.
Status¶
You can check the status of a Room Server connection with the IsConnected
flag:
bool isConnected = StrixNetwork.instance.roomSession.IsConnected;
If the client is connected to a room server, this property returns true.
Timeout of a room server connection¶
Room Server connections will timeout and will be disconnected if no activity is observed for certain time. Unlike a Master Server connection, a Room Server timeout is a hard loss of connection and will result in the player leaving a room on that server if they are in one.
Strix Unity SDK automatically sends heartbeat messages to a room server periodically. The default configuration is that the heartbeat period is 10 seconds and the server’s timeout is 120 seconds, so the room server connection would not timeout under a normal course of operation.
Note
The heartbeat timeout can be configured on the server-side, and you can also change the heartbeat period using the SDK (See SyncTimeClient for details). Please be careful when you change these values because inappropriate settings could lead to unintentional room server disconnection.