gpt4 book ai didi

c# - Client-Unet 中未收到广播

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:19 26 4
gpt4 key购买 nike

我正在使用 unet 进行客户端-服务器通信,当我从服务器(在 PC 上)向客户端(在 PC 上)广播消息时它工作正常但是当我尝试从服务器(PC)到客户端(Hololens 模拟器) unet 不起作用。

网络客户端.cs

public override void OnReceivedBroadcast(string fromAddress, string data)
{
fromAddress1 = fromAddress;
portnum = data;
Connect_with_server();
}
void Connect_with_server()
{
myclient = new NetworkClient();
myclient.RegisterHandler(MsgType.Connect, OnConnected);
myclient.RegisterHandler(MsgType.Disconnect, OnDisconnected);
myclient.RegisterHandler(ChatMsg.PlayPauseID, redgreen_val_received);
myclient.Connect(fromAddress1, Convert.ToInt32(portnum));
}

服务器.cs

void Start()
{
startServer();
}
public void startServer()
{
int serverPort = createServer();
if (serverPort != -1)
{
Debug.Log("Server created on port : " + serverPort);

discovery.broadcastData = serverPort.ToString();
discovery.Initialize();
discovery.StartAsServer();
}
else
{
Debug.Log("Failed to create Server");
}
}


int minPort = 10000;
int maxPort = 10010;
int defaultPort = 10000;
private int createServer()
{
int serverPort = -1;
bool serverCreated = NetworkServer.Listen(defaultPort);
if (serverCreated)
{
serverPort = defaultPort;
}
else
{
Debug.Log("Failed to create with the default port");
for (int tempPort = minPort; tempPort <= maxPort; tempPort++)
{
if (tempPort != defaultPort)
{
if (NetworkServer.Listen(tempPort))
{
serverPort = tempPort;
break;
}
if (tempPort == maxPort)
{
Debug.LogError("Failed to create server");
}
}
}
}
return serverPort;
}

void OnClientConnected(NetworkMessage netMessage)
{
MyNetworkMessage messageContainer = new MyNetworkMessage();
messageContainer.message = "Thanks for joining Mr.Client!";
Debug.Log("Client connected: ");
}

PS:我在UWP能力设置里开启了Private network client-server,在Player设置里也开启了PrivateNetworkClientServer和InternetClientServer。

最佳答案

可能不是您问题的直接解决方案,但是当我使用来自 Unity 的 UNET 的网络广播时,HoloLens 经常会莫名其妙地突然停止发现广播。我只需关闭并重新打开 HoloLens(长按电源按钮),问题就解决了。

关于c# - Client-Unet 中未收到广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57202601/

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