简体   繁体   English

如何知道客户端和服务器在Unity Networking中是否彼此连接

[英]How to know wether the client and server are connected to each other in Unity Networking

Here I am creating a multiplayer game with Unity Networking. 在这里,我正在使用Unity Networking创建多人游戏。 What I want is to start the game when both Server and client is connected. 我想要的是在服务器和客户端都连接时开始游戏。 In my game I have 2 players one will act as a server who hosts the game and the other acts as a client who connects to that host with the servers IPAddress. 在我的游戏中,我有2个玩家,一个将充当托管游戏的服务器,另一个将充当通过IPAddress服务器连接至该主机的客户端。 So when both are connected and ready to play i want to start the game. 因此,当两者都连接并准备好玩时,我想开始游戏。 Can anyone help me how can i get to know that both are connected. 谁能帮助我,我如何才能知道两者均已连接。

Here is the code what I am using. 这是我正在使用的代码。

public NetworkManager manager;

public void startServerr()
{
NetworkServer.Listen(9000);
    NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
    NetworkServer.RegisterHandler(MsgType.Disconnect, OnDisconnected);
    NetworkServer.RegisterHandler(MsgType.Error, OnError);
    manager.StartHost();
}

public void connectToServer()
{
    manager.networkAddress = PlayerPrefs.GetString("oppPlayerIP");
    manager.StartClient();
}

public void OnConnected(NetworkMessage netMsg)
{
    Debug.Log("Client Connected");
}

public void OnDisconnected(NetworkMessage netMsg)
{
    Debug.Log("Disconnected");
}

public void OnError(NetworkMessage netMsg)
{
    Debug.Log("Error while connecting");
}

You are currently using NetworkManager ... If you prefer to use NetworkManager over NetworkServer and NetworkClient that requires manual registering of the events, then your script must inherit from NetworkManager . 当前正在使用NetworkManager ...如果您希望通过NetworkServerNetworkClient使用NetworkManager ,而需要手动注册事件,则您的脚本必须继承自NetworkManager

Once you inherit from NetworkManager , you can then override and use OnClientConnect and OnServerConnect functions to check when client is connect to server and when server is connected to client respectively. NetworkManager继承后,即可覆盖并使用OnClientConnectOnServerConnect函数分别检查客户端何时连接到服务器以及服务器何时连接到客户端。

Here is an example: 这是一个例子:

public class NetTest : NetworkManager
{
    private NetworkManager manager;

    public void startServerr()
    {
        manager = this;
        manager.StartHost();
    }

    public void connectToServer()
    {
        //manager.networkAddress = PlayerPrefs.GetString("oppPlayerIP");
        manager.StartClient();
    }

    public override void OnClientConnect(NetworkConnection conn)
    {
        //base.OnClientConnect(conn);
    }

    public override void OnClientDisconnect(NetworkConnection conn)
    {
        //base.OnClientDisconnect(conn);
    }
    public override void OnServerConnect(NetworkConnection conn)
    {
        //base.OnServerConnect(conn);
    }

    public override void OnServerDisconnect(NetworkConnection conn)
    {
        //base.OnServerDisconnect(conn);
    }
}

You can find more network callback functions in the public function section here . 您可以在此处的公共功能部分找到更多的网络回调函数。

First of I recommend you reading all the documentation about unity networking before actually trying to make a connection. 首先,我建议您在实际尝试建立连接之前阅读所有有关统一网络的文档。 After you've done that you should know how to handle this request. 完成之后,您应该知道如何处理此请求。 If you don't know by then here is how to handle this specific request. 如果您不知道,那么这里是如何处理此特定请求的方法。

First you need to let the server handle all incoming connections by using the method OnConnected(NetworkMessage netMsg) . 首先,您需要使用OnConnected(NetworkMessage netMsg)方法让服务器处理所有传入连接。 You should be able to recognize the individual players connected to the server. 您应该能够识别连接到服务器的各个播放器。 Now you only need to create a ready button and send the ready status towards the server when clicked. 现在,您只需要创建一个就绪按钮,并在单击时将就绪状态发送到服务器即可。 If every player connected has send his isReady message towards the server you can start the game. 如果每个连接的玩家都向服务器发送了isReady消息,则可以开始游戏。 this is done by sending a message from the server towards all the connected clients. 这是通过从服务器向所有连接的客户端发送消息来完成的。

Hope this helps, any questions just ask. 希望这对您有帮助,任何问题都可以提出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM