Unity Mirror - NetworkServer向目标客户端发送消息
Unity Mirror - NetworkServer Send Message To Target Client
我不确定我在这里做错了什么,但我似乎无法将消息从服务器发送到客户端。这是我目前所拥有的:
protected virtual void RegisterHandlers(bool enable)
{
if (enable)
{
NetworkServer.RegisterHandler<ClientRequestLoadScene>(OnClientRequestedToLoadScene);
NetworkClient.RegisterHandler<ServerRequestLoadScene>(OnServerRequestLoadScene);
}
else
{
NetworkServer.UnregisterHandler<ClientRequestLoadScene>();
NetworkClient.UnregisterHandler<ServerRequestLoadScene>();
}
}
当实例开始注册新的处理程序时调用上面的方法。然后我有客户电话:
ClientRequestLoadScene msg = new ClientRequestLoadScene();
msg.scene = scene;
NetworkClient.Send(msg);
服务器正常接收。然后服务器运行以下命令:
private void OnClientRequestedToLoadScene(NetworkConnection conn, ClientRequestLoadScene msg)
{
...
...
ServerRequestLoadScene server_msg = new ServerRequestLoadScene();
server_msg.scene = msg.scene;
NetworkServer.SendToClientOfPlayer(conn.identity, msg);
...
...
}
客户端从未收到上述消息。我也试过: NetworkServer.SendToAll(msg);
并且客户也从未收到过。我做错了什么?
上述问题与这些行有关:
server_msg.scene = msg.scene;
NetworkServer.SendToClientOfPlayer(conn.identity, msg);
它需要是:
server_msg.scene = msg.scene;
conn.Send(server_msg);
我不确定我在这里做错了什么,但我似乎无法将消息从服务器发送到客户端。这是我目前所拥有的:
protected virtual void RegisterHandlers(bool enable)
{
if (enable)
{
NetworkServer.RegisterHandler<ClientRequestLoadScene>(OnClientRequestedToLoadScene);
NetworkClient.RegisterHandler<ServerRequestLoadScene>(OnServerRequestLoadScene);
}
else
{
NetworkServer.UnregisterHandler<ClientRequestLoadScene>();
NetworkClient.UnregisterHandler<ServerRequestLoadScene>();
}
}
当实例开始注册新的处理程序时调用上面的方法。然后我有客户电话:
ClientRequestLoadScene msg = new ClientRequestLoadScene();
msg.scene = scene;
NetworkClient.Send(msg);
服务器正常接收。然后服务器运行以下命令:
private void OnClientRequestedToLoadScene(NetworkConnection conn, ClientRequestLoadScene msg)
{
...
...
ServerRequestLoadScene server_msg = new ServerRequestLoadScene();
server_msg.scene = msg.scene;
NetworkServer.SendToClientOfPlayer(conn.identity, msg);
...
...
}
客户端从未收到上述消息。我也试过: NetworkServer.SendToAll(msg);
并且客户也从未收到过。我做错了什么?
上述问题与这些行有关:
server_msg.scene = msg.scene;
NetworkServer.SendToClientOfPlayer(conn.identity, msg);
它需要是:
server_msg.scene = msg.scene;
conn.Send(server_msg);