zoukankan      html  css  js  c++  java
  • NetWork自动连接代码

    DontDestroyOnLoad(this);

    var gameName = "You must change this";

    var serverPort = 25002;

    private var timeoutHostList = 0.0;

    private var lastHostListRequest = -1000.0;

    private var hostListRefreshTimeout = 10.0;

    private var connectionTestResult : ConnectionTesterStatus = ConnectionTesterStatus.Undetermined;//连接结果

    private var filterNATHosts = false;

    private var probingPublicIP = false;

    private var doneTesting = false;

    private var timer : float = 0.0;

    private var useNat = false;  // Should the server enabled NAT punchthrough feature

    private var windowRect;

    private var serverListRect;

    private var hideTest = false;

    private var testMessage = "Undetermined NAT capabilities";

    // Enable this if not running a client on the server machine

    //MasterServer.dedicatedServer = true;

    //没有连上主服务器报错信息

    function OnFailedToConnectToMasterServer(info: NetworkConnectionError)

    {

     Debug.Log(info);

    }

    //连接失败的报错信息

    function OnFailedToConnect(info: NetworkConnectionError)

    {

     Debug.Log(info);

    }

    function OnGUI ()

    {

        //服务端控制UI

     windowRect = GUILayout.Window (0, windowRect, MakeWindow, "Server Controls");

     //客户端显示服务端列表的UI

     //判断连接类型是没连接 和主服务PollHostList不是空的

     if (Network.peerType == NetworkPeerType.Disconnected && MasterServer.PollHostList().Length != 0)

      serverListRect = GUILayout.Window(1, serverListRect, MakeClientWindow, "Server List");

    }

    //游戏运行前

    function Awake ()

    {

     windowRect = Rect(Screen.width-300,0,300,100);//服务端控制UI面板大小

     //客户端显示服务端列表的UI的面板大小

     serverListRect = Rect(0, 0, Screen.width - windowRect.width, 100);

     // Start connection test

     connectionTestResult = Network.TestConnection();//测试网络连接结果

     // What kind of IP does this machine have? TestConnection also indicates this in the

     // test results

     //测试机器的IP类型、并打印出结果

     if (Network.HavePublicAddress())

      Debug.Log("This machine has a public IP address");

     else

      Debug.Log("This machine has a private IP address");

    }

    function Update()

    {

     // If test is undetermined, keep running

     if (!doneTesting)

      TestConnection();

    }

    //测试网络连接

    function TestConnection()

    {

     // Start/Poll the connection test, report the results in a label and react to the results accordingly

     connectionTestResult = Network.TestConnection();//测试网络连接结果

     //几种连接测试结果

     switch (connectionTestResult)

     {

      case ConnectionTesterStatus.Error: //连接错误

       testMessage = "Problem determining NAT capabilities";

       doneTesting = true;

       break;

       

      case ConnectionTesterStatus.Undetermined: //测试连接没结束

       testMessage = "Undetermined NAT capabilities";

       doneTesting = false;

       break;

          

      case ConnectionTesterStatus.PublicIPIsConnectable: //直接连接上了公共IP

       testMessage = "Directly connectable public IP address.";

       useNat = false;

       doneTesting = true;

       break;

       

      // This case is a bit special as we now need to check if we can

      // circumvent the blocking by using NAT punchthrough

      case ConnectionTesterStatus.PublicIPPortBlocked://如果IP端口被阻拦 防火墙那些东东

       testMessage = "Non-connectble public IP address (port " + serverPort +" blocked), running a server is impossible.";

       useNat = false;

       // If no NAT punchthrough test has been performed on this public IP, force a test

       if (!probingPublicIP)

       {

        Debug.Log("Testing if firewall can be circumvented");

        connectionTestResult = Network.TestConnectionNAT();

        probingPublicIP = true;

        timer = Time.time + 10;

       }

       // NAT punchthrough test was performed but we still get blocked

       else if (Time.time > timer)

       {

        probingPublicIP = false;   // reset

        useNat = true;

        doneTesting = true;

       }

       break;

      case ConnectionTesterStatus.PublicIPNoServerStarted://没有服务开启

       testMessage = "Public IP address but server not initialized, it must be started to check server accessibility. Restart connection test when ready.";

       break;

       

      case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted://限制了NAT

       Debug.Log("LimitedNATPunchthroughPortRestricted");

       testMessage = "Limited NAT punchthrough capabilities. Cannot connect to all types of NAT servers.";

       useNat = true;

       doneTesting = true;

       break;

         

      case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric://限制了NAT

       Debug.Log("LimitedNATPunchthroughSymmetric");

       testMessage = "Limited NAT punchthrough capabilities. Cannot connect to all types of NAT servers. Running a server is ill adviced as not everyone can connect.";

       useNat = true;

       doneTesting = true;

       break;

      

      case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:

      case ConnectionTesterStatus.NATpunchthroughFullCone://通过连接

       Debug.Log("NATpunchthroughAddressRestrictedCone || NATpunchthroughFullCone");

       testMessage = "NAT punchthrough capable. Can connect to all servers and receive connections from all clients. Enabling NAT punchthrough functionality.";

       useNat = true;

       doneTesting = true;

       break;

      default:

       testMessage = "Error in test routine, got " + connectionTestResult;//输出的测试内容显示在面板上的

     }

     //Debug.Log(connectionTestResult + " " + probingPublicIP + " " + doneTesting);

    }

    //服务端控制UI面板内的控件内容

    function MakeWindow (id : int)

     hideTest = GUILayout.Toggle(hideTest, "Hide test info");

     if (!hideTest)

     {

      GUILayout.Label(testMessage);//显示上面的测试内容

      //刷新连接

      if (GUILayout.Button ("Retest connection"))

      {

       Debug.Log("Redoing connection test");

       probingPublicIP = false;

       doneTesting = false;

       connectionTestResult = Network.TestConnection(true);

      }

     }

     //开启服务端的UI

     if (Network.peerType == NetworkPeerType.Disconnected)

     {

      GUILayout.BeginHorizontal();

      GUILayout.Space(10);

      // 开启服务

      if (GUILayout.Button ("Start Server"))

      {

       Network.InitializeServer(32, serverPort, useNat);

       MasterServer.RegisterHost(gameName, "stuff", "l33t game for all");//这个要注意下  游戏名称 类型等

      }

      // 刷新 hosts

      if (GUILayout.Button ("Refresh available Servers") || Time.realtimeSinceStartup > lastHostListRequest + hostListRefreshTimeout)

      {

       MasterServer.RequestHostList (gameName);

       lastHostListRequest = Time.realtimeSinceStartup;

      }

      

      GUILayout.FlexibleSpace();

      

      GUILayout.EndHorizontal();

     }

     else

     {

         //关闭服务

      if (GUILayout.Button ("Disconnect"))

      {

       Network.Disconnect();

       MasterServer.UnregisterHost();

      }

      GUILayout.FlexibleSpace();

     }

     GUI.DragWindow (Rect (0,0,1000,1000));

    }

    //客户端显示服务端列表的UI面板的UI内容、自动刷出服务列表

    function MakeClientWindow(id : int)

    {

     GUILayout.Space(5);

     var data : HostData[] = MasterServer.PollHostList();

     var count = 0;

     for (var element in data)

     {

      GUILayout.BeginHorizontal();

      // Do not display NAT enabled games if we cannot do NAT punchthrough

      if ( !(filterNATHosts && element.useNat) )

      {

       var connections = element.connectedPlayers + "/" + element.playerLimit;

       GUILayout.Label(element.gameName);

       GUILayout.Space(5);

       GUILayout.Label(connections);

       GUILayout.Space(5);

       var hostInfo = "";

       

       // Indicate if NAT punchthrough will be performed, omit showing GUID

       if (element.useNat)

       {

        GUILayout.Label("NAT");

        GUILayout.Space(5);

       }

       // Here we display all IP addresses, there can be multiple in cases where

       // internal LAN connections are being attempted. In the GUI we could just display

       // the first one in order not confuse the end user, but internally Unity will

       // do a connection check on all IP addresses in the element.ip list, and connect to the

       // first valid one.

       for (var host in element.ip)

        hostInfo = hostInfo + host + ":" + element.port + " ";

       

       //GUILayout.Label("[" + element.ip + ":" + element.port + "]"); 

       GUILayout.Label(hostInfo); 

       GUILayout.Space(5);

       GUILayout.Label(element.comment);

       GUILayout.Space(5);

       GUILayout.FlexibleSpace();

       if (GUILayout.Button("Connect"))

        Network.Connect(element);

      }

      GUILayout.EndHorizontal(); 

     }

    }

  • 相关阅读:
    centos安装pip
    centos修改国内镜像源
    centos配置snmp服务
    django使用ModelForm上传文件
    Vue slot
    umi3.2+ targets ie不生效的问题
    mongo环境快速搭建工具 mlaunch
    mac上常用软件
    磁盘性能测试工具 iozone
    磁盘性能测试工具 bonnie++
  • 原文地址:https://www.cnblogs.com/softimagewht/p/2159396.html
Copyright © 2011-2022 走看看