1

我正在使用 Lidgren 将统一游戏连接到游戏服务器。一切正常。唯一的问题是当客户端断开与服务器的连接时(由于失去互联网连接甚至手动),它无法再次连接。我必须关闭并重新打开游戏才能再次连接。我也在统一之外尝试过这个并得到了相同的结果:

连接方法是这样的:

public IEnumerator Connect()
{
    m_Client.Start();
    var hail = m_Client.CreateMessage();
    AuthRequest id = new AuthRequest()
    {
        Name = PlayerSettings.Name,
    };

    hail.Write(new Packet(PacketType.AuthRequest, id).SerializePacket());
    m_Client.Connect(k_ServerAddress, k_Port, hail);
    Debug.Log("Connecting " + id.Name + " to " + k_ServerAddress + ":" + k_Port);

    NetConnectionStatus status = m_Client.ConnectionStatus;
    while (m_Client.ConnectionStatus != NetConnectionStatus.Disconnected)
    {
        if (m_Client.ConnectionStatus != status)
        {
            status = m_Client.ConnectionStatus;
            Debug.Log("Status: " + status);
        }
        yield return null;
    }

}

如果我使用此代码:

LidgrenClient m_Client = new LidgrenClient();
m_Client.Connect();

客户端已连接,但是当客户端与服务器断开连接时,下面的更新方法仅记录每 5 分钟尝试重新连接一次,但实际上什么也不做:

void Update()
{
    if (!waiting && !m_Client.Connected)
    {
        Debug.Log("Trying to Reconnect");
        StartCoroutine(TryReconnect());
    }
    m_Client.ReadMessages();
}
IEnumerator TryReconnect()
{
    waiting = true;
    m_Client.Connect();
    yield return new WaitForSeconds(5f);
    waiting = false;
}
4

0 回答 0