gpt4 book ai didi

c# - Unity如何使用NetworkDiscovery从客户端查找服务器

转载 作者:行者123 更新时间:2023-12-02 08:40:29 25 4
gpt4 key购买 nike

[我用 C# 工作]

为了决定设备应该成为具有本地客户端或仅客户端的服务器(对于无主服务器网络),我需要在变成具有以下功能的服务器之前找出该设备是否有任何其他可用的服务器本地客户端,如果不是另一个客户端设备通过从服务器接收 NetworkDiscovery 广播或不加入来加入 - 目前我无法让客户端从服务器接收广播。

我创建了两个空的游戏对象,每个都带有脚本,一个脚本使其成为服务器,并且应该通过 NetworkDiscovery.StartAsServer() 从其 NetworkDiscovery 进行广播,但目前我的一个脚本NetworkDiscovery 已设置为 NetworkDiscovery.StartAsClient() 的客户端未调用 OnRecievedBroadcast() 函数,因此未接收来自服务器的广播。

下面显示的两个脚本是客户端和服务器:

客户端 -

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class SearchForServers : MonoBehaviour {
bool serverIsFound;
NetworkDiscovery NetworkDiscovery;
// Use this for initialization
void Start () {
serverIsFound = false;
NetworkClient Client = new NetworkClient();
NetworkDiscovery = gameObject.AddComponent<NetworkDiscovery>();
NetworkDiscovery.Initialize();
NetworkDiscovery.StartAsClient();
}

void OnRecievedBroadcast(string fromAdress, string data)
{
serverIsFound = true;
}

// Update is called once per frame
void Update () {
Debug.Log("Server Found = " + serverIsFound);
}
}

服务器 -

    using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class CreateServer : MonoBehaviour
{
bool create;
int minPort = 10000;
int maxPort = 10010;
int defaultPort = 10000;
NetworkDiscovery NetworkDiscovery;

void Start()
{
create = true;
if (create == true)
{
int serverPort = createServer();
if (serverPort != -1)
{
Debug.Log("Server created on port : " + serverPort);
}
else
{
Debug.Log("Failed to create Server");
}
}
NetworkDiscovery = gameObject.AddComponent<NetworkDiscovery>();
NetworkDiscovery.Initialize();
NetworkDiscovery.StartAsServer();
}


//Creates a server then returns the port the server is created with. Returns -1 if server is not created
int createServer()
{
int serverPort = -1;
//Connect to default port

bool serverCreated = NetworkServer.Listen(defaultPort);
if (serverCreated)
{
serverPort = defaultPort;
Debug.Log("Server Created with default port");
}
else
{
Debug.Log("Failed to create with the default port");
//Try to create server with other port from min to max except the default port which we trid already
for (int tempPort = minPort; tempPort <= maxPort; tempPort++)
{
//Skip the default port since we have already tried it
if (tempPort != defaultPort)
{
//Exit loop if successfully create a server
if (NetworkServer.Listen(tempPort))
{
serverPort = tempPort;
break;
}

//If this is the max port and server is not still created, show, failed to create server error
if (tempPort == maxPort)
{
Debug.LogError("Failed to create server");
}
}
}
}
return serverPort;
}
}

这些是带有调试日志记录的控制台日志

enter image description here

感谢您的阅读,我希望您能够意识到我哪里出了问题,或者确实如何完成上述任务。 @程序员

最佳答案

首先,您将每个服务器和客户端脚本命名为相同的名称。您不需要在服务器代码中使用OnRecievedBroadcast。您只需要在客户端代码中使用它。客户端未调用 OnRecievedBroadcast,因为您没有覆盖 NetworkDiscovery。您必须覆盖它。

这是一个简单的发现代码:

客户:

public class NetClient : NetworkDiscovery
{

void Start()
{
startClient();
}

public void startClient()
{
Initialize();
StartAsClient();
}

public override void OnReceivedBroadcast(string fromAddress, string data)
{
Debug.Log("Server Found");
}
}

服务器:

public class NetServer : NetworkDiscovery
{
// Use this for initialization
void Start()
{
Application.runInBackground = true;
startServer();
}

//Call to create a server
public void startServer()
{
int serverPort = createServer();
if (serverPort != -1)
{
Debug.Log("Server created on port : " + serverPort);
broadcastData = serverPort.ToString();
Initialize();
StartAsServer();
}
else
{
Debug.Log("Failed to create Server");
}
}

int minPort = 10000;
int maxPort = 10010;
int defaultPort = 10000;

//Creates a server then returns the port the server is created with. Returns -1 if server is not created
private int createServer()
{
int serverPort = -1;
//Connect to default port
bool serverCreated = NetworkServer.Listen(defaultPort);
if (serverCreated)
{
serverPort = defaultPort;
Debug.Log("Server Created with deafault port");
}
else
{
Debug.Log("Failed to create with the default port");
//Try to create server with other port from min to max except the default port which we trid already
for (int tempPort = minPort; tempPort <= maxPort; tempPort++)
{
//Skip the default port since we have already tried it
if (tempPort != defaultPort)
{
//Exit loop if successfully create a server
if (NetworkServer.Listen(tempPort))
{
serverPort = tempPort;
break;
}

//If this is the max port and server is not still created, show, failed to create server error
if (tempPort == maxPort)
{
Debug.LogError("Failed to create server");
}
}
}
}
return serverPort;
}
}

createServer() 函数是您上一个问题中的函数。我认为您不能在编辑器中同时运行服务器和客户端代码。

为了进行测试,您必须在启用服务器代码/NetServer 的电脑中构建代码。运行构建的程序,然后从编辑器中运行客户端/NetClient 代码。这应该会在服务器上发现游戏。

关于c# - Unity如何使用NetworkDiscovery从客户端查找服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38676231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com