c# - 如果 Photon Network 突然断开或用户将网络从 wifi 切换到 Unity3d 中的数据,如何快速重新连接?
问题描述
我开发了一个像 8 球台球这样的游戏。我正在使用光子网络进行多人游戏功能。游戏运行良好,但在某些情况下除外:
- 光子网络突然断开。
- 如果用户尝试将网络(例如 WiFi)切换到移动数据,则无法再连接 Photon 网络(由于 IP 地址更改)。
为了解决这个问题,我尝试了这个:
public override void OnDisconnectedFromPhoton() {
Debug.Log("Disconnected from photon");
PhotonNetwork.ReconnectAndRejoin();
}
但它不能正常工作。
对于配对,我是这样写的:
public void JoinRoomAndStartGame()
{
ExitGames.Client.Photon.Hashtable expectedCustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "tbl", GameManager.Instance.tableNumber }, { "isAvailable", true} };
PhotonNetwork.JoinRandomRoom(expectedCustomRoomProperties, 0);
}
public void OnPhotonRandomJoinFailed()
{
RoomOptions roomOptions = new RoomOptions();
roomOptions.PlayerTtl = 15000;
roomOptions.CustomRoomPropertiesForLobby = new String[] { "tbl", "isAvailable" };
roomOptions.CustomRoomProperties = new ExitGames.Client.Photon.Hashtable() { { "tbl", GameManager.Instance.tableNumber }, { "isAvailable", true} };
roomOptions.MaxPlayers = 2;
roomOptions.IsVisible = true;
PhotonNetwork.CreateRoom(null, roomOptions, TypedLobby.Default);
}
我是光子新手。在这些情况下如何正确重新连接?
解决方案
public override void OnDisconnected(DisconnectCause cause)
{
StartCoroutine(MainReconnect());
}
private IEnumerator MainReconnect()
{
while (PhotonNetwork.NetworkingClient.LoadBalancingPeer.PeerState != ExitGames.Client.Photon.PeerStateValue.Disconnected)
{
Debug.Log("Waiting for client to be fully disconnected..", this);
yield return new WaitForSeconds(0.2f);
}
Debug.Log("Client is disconnected!", this);
if (!PhotonNetwork.ReconnectAndRejoin())
{
if (PhotonNetwork.Reconnect())
{
Debug.Log("Successful reconnected!", this);
}
}
else
{
Debug.Log("Successful reconnected and joined!", this);
}
}
推荐阅读
- javascript - TypeError:无法读取未定义的属性“brewery_type”
- apache-spark - 没有看到 spark-submit 的输出
- reactjs - React 路由器不适用于组件
- php - 您将如何托管仅适用于少数用户的私有 Web 应用程序?
- http - -d 在 curl 请求中代表什么?
- angular - Angular,获取父路由组件
- image-processing - 应用降噪滤波器(例如高斯滤波器)是否会使图像更好地进行分析?
- linux-kernel - 驱动程序是否需要包含 DTS 文件?
- sql - 如何在 SQL 中为前 100 或 5 行设置默认约束
- c# - 如何在 C# 中通过 XMLSerializer 序列化控件