the mentioned code works fine for the first time that the client wants to connect to the server . when I interrupt the internet for 30 s unity transport server disconnect the client when I try to reconnect the client to the server with this code :
if (!_serverEndPoint.IsValid)
{
_networkConnection = _clientDrive.Connect(_serverEndPoint);
print("Re-connecting...");
}
If you want to check if a connection is alive, you should use the NetworkDriver.GetConnectionState method. Checking IsValid on a network endpoint will only tell you if that endpoint is well-formatted (basically if the IP address is valid).
if (_clientDrive.GetConnectionState(_networkconnection) == NetworkConnection.State.Disconnected)
{
print("client is disconnected.....");
_clientDisConnectedOnce = true;
}
if (_clientDrive.GetConnectionState(_networkconnection) == NetworkConnection.State.Connecting && _clientDisConnectedOnce)
{
print("client is connecting.....");
_networkconnection = _clientDrive.Connect(_serverEndPoint);
}
I let the client connect first then I closed the server and I wait until the client disconnect completely, the first problem is it takes about 30 s until _clientDrive.GetConnectionState(_networkconnection) find out client has disconnected. and 30s is too much for an online multiplayer game. is there any faster way to find out the server is faced with a problem and let the client know?
the second problem is when the client disconnected, it wouldn’t reconnect with this code could you guide me on how to solve it?
If your server exits gracefully, it can call Disconnect on all its active connections and the clients will be notified without delay (well, with the delay it takes for the message to make it to them).
If your server doesn’t exit gracefully (say it crashes), then the 30 seconds timeout can be reduced. With the new settings API (version 1.0.0-pre.8 or above), you can configure it in the driver like so:
var settings = new NetworkSettings();
settings.WithNetworkConfigParameters(disconnectTimeoutMS: 2000);
var driver = NetworkDriver.Create(settings);
This would set the timeout to 2 seconds.
Is there any reason you’re checking for the Connecting state in the second conditional statement? That state only occurs after calling Connect and before the connection handshake completes. Once the handshake is completed, the state becomes Connected. And once the connection fails, it becomes Disconnected.
thank you man , that’s worked perfectly.
when server crashes the client connection will completely lost,for Reconnecting I used _networkconnection = _clientDrive.Connect(_serverEndPoint); but it didn’t work , I initialized the connection and it worked correctly.
Unfortunately, no. Periodically attempting to reconnect to the server is typically how it’s done.
Same way as on the client. The disconnect timeout setting can also be applied to the server. Just need to apply the setting when creating the server’s driver.