如何在统一网络代码中停止主机或客户端?
当我断开服务器连接并转到菜单场景时,出现两个 NetworkManager 对象,但它们正在实现单例模式,它本身意味着存在两个相同的对象物体不应该发生。
然后我尝试在断开连接时手动销毁NetworkManager,它对客户端工作正常,但仅一次(仅在第一次断开连接时)以及当客户端进一步连接时(此处为客户端)正在连接大厅(这是一个不同的场景),它会抛出一些错误,并且无法再次离开场景,对于服务器端,这甚至一次都不起作用。
这是我第二次重新连接时在客户端实例中收到的错误消息 -:
这是服务器断开连接代码 -:
public void OnServerDisconnect()
{
if (!IsServer) { return; }
List<NetworkClient> connectedPlayers = (List<NetworkClient>)NetworkManager.Singleton.ConnectedClientsList;
for (int i = 0; i < connectedPlayers.Count; i++)
{
NetworkClient player = connectedPlayers[i];
if(player.ClientId == NetworkManager.Singleton.ServerClientId) { continue; }
else
{
ClientData.Remove(player.ClientId);
ChangeClietSceneClientRpc();
}
}
ClientData.Remove(NetworkManager.Singleton.ServerClientId);
NetworkManager.Singleton.Shutdown(true);
Destroy(NetworkManager.Singleton.gameObject);
SceneManager.LoadScene("StartScene", LoadSceneMode.Single);
}
[ClientRpc]
private void ChangeClietSceneClientRpc()
{
if (IsHost) { return; }
NetworkManager.Singleton.Shutdown(true);
Destroy(NetworkManager.Singleton.gameObject);
SceneManager.LoadScene("StartScene", LoadSceneMode.Single);
}
这是客户端断开连接代码 -:< /强>
public void OnClientDisconnect(ulong clientId)
{
if (!IsServer) { return; }
else
{
ClientData.Remove(clientId);
ChangeClietSceneClientRpc();
}
}
When I disconnect my server and go to the menu scene, there comes two NetworkManager Objects but a they are implementing the Singleton pattern
and it in itself means that this prescense of two same objects should not happen.
Then I tried to manually destroy the NetworkManager upon disconnecting and it works fine for client but only for one time (upon first disconnecting only) and when furthur the client connects (Here client is connecting the lobby which is a different scene) it throws some errors and is not able to leave the scene again and for server side this does'nt even work once.
This is the error messages I get in the client instance upon reconnecting second time -:
This is Server disconnect code -:
public void OnServerDisconnect()
{
if (!IsServer) { return; }
List<NetworkClient> connectedPlayers = (List<NetworkClient>)NetworkManager.Singleton.ConnectedClientsList;
for (int i = 0; i < connectedPlayers.Count; i++)
{
NetworkClient player = connectedPlayers[i];
if(player.ClientId == NetworkManager.Singleton.ServerClientId) { continue; }
else
{
ClientData.Remove(player.ClientId);
ChangeClietSceneClientRpc();
}
}
ClientData.Remove(NetworkManager.Singleton.ServerClientId);
NetworkManager.Singleton.Shutdown(true);
Destroy(NetworkManager.Singleton.gameObject);
SceneManager.LoadScene("StartScene", LoadSceneMode.Single);
}
[ClientRpc]
private void ChangeClietSceneClientRpc()
{
if (IsHost) { return; }
NetworkManager.Singleton.Shutdown(true);
Destroy(NetworkManager.Singleton.gameObject);
SceneManager.LoadScene("StartScene", LoadSceneMode.Single);
}
This is Client disconnect code -:
public void OnClientDisconnect(ulong clientId)
{
if (!IsServer) { return; }
else
{
ClientData.Remove(clientId);
ChangeClietSceneClientRpc();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您的网络对象未通过 NetworkObject.Spawn() 实例化时(如果您没有使用最新的网络代码版本),就会发生此错误。我也对客户端断开连接和重新连接进行了一些测试。我发现在网络代码预览5中,最初放置在场景中的所有网络对象都能够在第一时间连接和同步,但一旦客户端尝试重新连接它就会失去连接。当我在预览版8中测试时,将不再出现此错误。
不确定这是否是您的情况,但您可以尝试一下。
This error occured when your network object was not instantiate through NetworkObject.Spawn() (if you are not using the latest netcode version). I have made a few test about client disconnect and reconnect as well. I found out that in netcode preview 5, all network object which were originally put in the scene will be able to connect and synchornize at the first time, but will lost connection once client trying to reconnect it. This error will no longer appear when I test it in preview 8.
Not sure whether this is your situation, but you can give it a try.