Search Unity

Need help with master server and php

Discussion in 'Multiplayer' started by dipixoft, Apr 25, 2015.

  1. dipixoft

    dipixoft

    Joined:
    Jan 17, 2014
    Posts:
    2
    Hi, my name is andys and i have a question, currently i'm working on a multiplayer game, i'm using php to store all the host data of the players and the match, everything is working fine, but now it says NAT target 2147483647 not connected to NAT facilitator 67.225.180.24:50005, i suppose that the ip shown there is the unity3d master server but i'm not sure, if that's the unity 3d master server ip it means that i'm using a kind of testing tool, so i'd like to know what way i have to host a server, can i just take some of my computers and keep them on and use them as servers or are there any other option, Actually im just using the unity networking, any answer will be appreciated and sorry, im noob ;)

    this is the connection script

    Code (JavaScript):
    1. import System.Collections.Generic;
    2. ///////////////////Connetion to server
    3.  
    4. var portString = "25565";
    5.  
    6. ///////////////////////////////////////////////////////////new version php
    7.  
    8. public var masterSeverURL : String;
    9. private var hostData : HostData[] = null;
    10.  
    11. ///////////////////////////////////////////////////////////new version php
    12. var playerName : String;
    13. var battleName : String;
    14.  
    15. public static var cManager : ConnectionScript;
    16. public var playerList = new List.<onlinePlayers>();
    17. var myOnlinePlayer : onlinePlayers;
    18. var myPlayer : GameObject;
    19. private var startedGame;
    20. private var createdPlayer : boolean = false;
    21.  
    22. private var gameName : String = "";
    23. private var gameType : String = "";
    24. private var Comment : String = "";
    25. var updateDelayTime : float = 10.0;
    26.  
    27. var playerBio : playerInfo;
    28.  
    29. var myPlayerID : NetworkView;
    30.  
    31. function Awake() {
    32.         DontDestroyOnLoad(gameObject);
    33.         playerBio = this.GetComponent.<playerInfo>();
    34.         cManager = this;
    35. }
    36.  
    37. public function PollHostList() : HostData[] {
    38.     return hostData;
    39. }
    40.  
    41. public function getServers(type : String) {
    42.    
    43.     var url = masterSeverURL + "/Ms/getMs.php?gameType=" + type;
    44.     Debug.Log("checando url " + url);
    45.     var www = new WWW (url);
    46.     yield www;
    47.    
    48.     if (www.text == "") {
    49.         hostData = null;
    50.         return;
    51.     }
    52.    
    53.     Debug.Log(www.text);
    54.    
    55.     var hosts = www.text.Split(";"[0]);
    56.     hostData = new HostData[hosts.length];
    57.     var index = 0;
    58.    
    59.     for (var host in  hosts) {
    60.         data = host.Split(","[0]);
    61.         hostData[index] = new HostData();
    62.         hostData[index].ip  = new String[1];
    63.         hostData[index].ip[0]  = data[0];      
    64.         hostData[index].port  = parseInt(data[1]);      
    65.         hostData[index].useNat  = (data[2] == "1");
    66.         hostData[index].guid  = data[3];
    67.         hostData[index].gameType  = data[4];
    68.         hostData[index].gameName  = data[5];
    69.         hostData[index].connectedPlayers  = parseInt( data[6]);
    70.         hostData[index].playerLimit  = parseInt(data[7]);
    71.         hostData[index].passwordProtected  = (data[8] == "1");
    72.         hostData[index].comment  = data[9];
    73.         index ++;      
    74.     }  
    75. }
    76.  
    77. function Start(){
    78.         startedGame = false;
    79. }
    80.  
    81. function Update(){
    82.     playerName = PlayerPrefs.GetString("MainPlayerName");
    83. }
    84.  
    85. function initiateServer(Rcomment : String, RgameType : String, RgameName : String, RmaxPlayers : int){
    86.         gameName = RgameName;
    87.         gameType = RgameType;
    88.         Comment = Rcomment;
    89.         Network.InitializeServer(RmaxPlayers, parseInt(portString), !Network.HavePublicAddress());
    90.         MasterServer.RegisterHost(RgameType, RgameName, Rcomment);
    91. }
    92.  
    93.    
    94. function registrationLoop() {
    95.     while (Network.isServer) {
    96.         var url = masterSeverURL + "/Ms/updateHost.php?";
    97.         url += "gameName=" + gameName;
    98.         url += "gameType=" + gameType;
    99.         Debug.Log(url);
    100.         var www = new WWW(url);
    101.         yield www;
    102.         yield WaitForSeconds(updateDelayTime);
    103.     }
    104. }
    105.  
    106. public function registerHost() {
    107.             while (Network.player.externalPort == 65535) {
    108.                 yield WaitForSeconds(1);
    109.             }
    110.             var url = masterSeverURL + "/Ms/registerG.php?";
    111.             url += "gameType=" + gameType;
    112.             url += "&gameName=" +  WWW.EscapeURL(gameName);
    113.             url += "&comment=" + Comment;
    114.             url += "&useNat=" + !Network.HavePublicAddress();
    115.             url += "&connectedPlayers=" + (Network.connections.length+1);
    116.             url += "&playerLimit=" + Network.maxConnections;
    117.             url += "&internalIp=" + Network.player.ipAddress;
    118.             url += "&internalPort=" + Network.player.port;
    119.             url += "&externalIp=" + Network.player.externalIP;
    120.             url += "&externalPort=" + Network.player.externalPort;
    121.             url += "&guid=" + Network.player.guid;
    122.             url += "&password=" + (Network.incomingPassword != "" ? 1 : 0);
    123.             Debug.Log(url);
    124.             var www = new WWW (url);
    125.             yield www;  
    126.             Debug.Log(www.text);
    127.             StartCoroutine(registrationLoop());
    128. }
    129.  
    130.  
    131. function OnPlayerConnected(id : NetworkPlayer) {
    132.    
    133.         var url = masterSeverURL + "/Ms/updatePlayers.php?";
    134.         url += "gameType=" + gameType;
    135.         url += "&gameName=" + gameName;
    136.         url += "&connectedPlayers=" + (Network.connections.length + 1);
    137.         Debug.Log("url " + url );
    138.         var www = new WWW (url);
    139.            
    140.    
    141.         for (var pl : onlinePlayers in playerList) {
    142.             GetComponent.<NetworkView>().RPC("updateList",id,pl.playerName,pl.onlinePlayer);
    143.         }
    144. }
    145.  
    146. function OnConnectedToServer() {
    147.     GetComponent.<NetworkView>().RPC("setServer",RPCMode.Server,playerName, Network.player);          
    148. }
    149.  
    150. function OnServerInitialized(){
    151.         Debug.Log("Servidor iniciado");
    152.         registerHost();
    153.         setServer(playerName, Network.player);
    154. }
    155.  
    156. function OnPlayerDisconnected(id : NetworkPlayer) {
    157.    
    158.     var url = masterSeverURL + "/Ms/updatePlayers.php?";
    159.     url += "gameName=" + gameName;
    160.     url += "&gameType=" + gameType;
    161.     url += "&connectedPlayers=" + Network.connections.length;
    162.     Debug.Log(url);
    163.     var www = new WWW(url);
    164.    
    165.     GetComponent.<NetworkView>().RPC("clean", RPCMode.All,id);
    166.     Network.RemoveRPCs(id);
    167.     Network.DestroyPlayerObjects(id);
    168. }
    169.  
    170. function OnDisconnectedFromServer() {
    171.     if (Network.isServer) {
    172.         var url = masterSeverURL + "/Ms/unregisterG.php?";
    173.         url += "gameName=" + gameName;
    174.         url += "&gameType=" + gameType;
    175.         var www =  new WWW(url);
    176.         yield www;
    177.     }
    178.    
    179.     playerList.Clear();
    180.     Application.LoadLevel(0);
    181.     Destroy(this.gameObject);
    182. }
    183.  
    184.  
    185. function OnGUI() {
    186.     if(startedGame){
    187.     if (GUI.Button(new Rect(Screen.width - 100, Screen.height - 50, 100, 25), "Entrar") && !createdPlayer) {
    188.         playerBio.spawn();
    189.         myPlayer = playerBio.myPlayer;
    190.         GetComponent.<NetworkView>().RPC("addPlayer", RPCMode.All, Network.player);  
    191.         createdPlayer = true;
    192.     }
    193.     if (GUI.Button(new Rect(Screen.width - 100, Screen.height - 25, 100, 25), "Desconectarse")) {
    194.         DisconnectNow();
    195.     }
    196.    
    197.     }
    198.  
    199.         GUI.Label(new Rect(0,Screen.height-25,Screen.width,25),"Current Status: " + testStatus);
    200.         GUI.Label(new Rect(0,Screen.height-50,Screen.width,25),"Test result : " + testMessage);
    201.         GUI.Label(new Rect(0,Screen.height-75,Screen.width,25),shouldEnableNatMessage);
    202.         if (!doneTesting)
    203.             TestConnection();
    204.        
    205. }
    206.  
    207.  
    208.  
    209.  
    210. @RPC
    211. function setServer(Pname : String, id : NetworkPlayer) {
    212.     GetComponent.<NetworkView>().RPC("updateList",RPCMode.All,Pname, id);  
    213. }
    214.  
    215. @RPC
    216. function updateList(newPlayerName : String, player : NetworkPlayer)
    217. {
    218.     var temp : onlinePlayers  = new onlinePlayers();
    219.     temp.playerName = newPlayerName;
    220.     temp.onlinePlayer = player;
    221.     playerList.Add(temp);
    222.     if(Network.player == player){
    223.         myOnlinePlayer = temp;
    224.     }
    225. }
    226.  
    227.  
    228. @RPC
    229. function clean(id : NetworkPlayer) {
    230.     var temp : onlinePlayers  = new onlinePlayers();
    231.     for (var pl :onlinePlayers in playerList) {
    232.         if (pl.onlinePlayer == id) {
    233.             temp = pl;
    234.         }
    235.     }
    236.     if (temp != null) {
    237.         playerList.Remove(temp);
    238.     }
    239. }
    240.  
    241. @RPC
    242. function startGame(level : String, levelPrefix : int) {
    243.         startedGame = true;
    244.         Network.SetSendingEnabled(0, false);
    245.         Network.isMessageQueueRunning = false;
    246.         Application.LoadLevel(level);
    247.         yield;
    248.         Network.isMessageQueueRunning = true;
    249.         Network.SetSendingEnabled(0, true);
    250.         this.GetComponent.<NetworkMenu>().enabled  = false;
    251. }
    252.  
    253.  
    254. @RPC
    255. function addPlayer(id : NetworkPlayer) {
    256.         for (var pl : onlinePlayers in playerList ) {
    257.             if (pl.onlinePlayer == id) {
    258.                 pl.playerMesh = playerBio.myPlayer;
    259.                 myPlayerID = pl.playerMesh.GetComponent.<NetworkView>();
    260.                 pl.playerMesh.name = pl.playerName;
    261.             }
    262.            
    263.         }
    264. }
    265. class onlinePlayers extends System.Object{
    266.         var playerName : String;
    267.         var onlinePlayer : NetworkPlayer;
    268.         var playerMesh : GameObject;
    269. }
    270.  
    271. function DisconnectNow() {
    272.     if (Network.isServer) {
    273.         var url = masterSeverURL + "/Ms/unregisterG.php?";
    274.         url += "gameName=" + gameName;
    275.         url += "&gameType=" + gameType;
    276.         var www =  new WWW(url);
    277.         yield www;
    278.     }
    279.     Network.Disconnect(200);  
    280. }
    281.  
    282.  
    283. //////////////////////////////////////////////////////////////////////////////////////////test connection
    284. var testStatus = "Testing network connection capabilities.";
    285.     var testMessage = "Test in progress";
    286.     var shouldEnableNatMessage : String = "";
    287.     var doneTesting = false;
    288.     var probingPublicIP = false;
    289.     var serverPort = 9999;
    290.     var connectionTestResult = ConnectionTesterStatus.Undetermined;
    291.    
    292.     // Indicates if the useNat parameter be enabled when starting a server
    293.     var useNat = false;
    294.    
    295.    
    296.     function TestConnection() {
    297.         // Start/Poll the connection test, report the results in a label and
    298.         // react to the results accordingly
    299.         connectionTestResult = Network.TestConnection();
    300.         switch (connectionTestResult) {
    301.             case ConnectionTesterStatus.Error:
    302.                 testMessage = "Problem determining NAT capabilities";
    303.                 doneTesting = true;
    304.                 break;
    305.                
    306.             case ConnectionTesterStatus.Undetermined:
    307.                 testMessage = "Undetermined NAT capabilities";
    308.                 doneTesting = false;
    309.                 break;
    310.                            
    311.             case ConnectionTesterStatus.PublicIPIsConnectable:
    312.                 testMessage = "Directly connectable public IP address.";
    313.                 useNat = false;
    314.                 doneTesting = true;
    315.                 break;
    316.                
    317.             // This case is a bit special as we now need to check if we can
    318.             // circumvent the blocking by using NAT punchthrough
    319.             case ConnectionTesterStatus.PublicIPPortBlocked:
    320.                 testMessage = "Non-connectable public IP address (port " +
    321.                     serverPort +" blocked), running a server is impossible.";
    322.                 useNat = false;
    323.                 // If no NAT punchthrough test has been performed on this public
    324.                 // IP, force a test
    325.                 if (!probingPublicIP) {
    326.                     connectionTestResult = Network.TestConnectionNAT();
    327.                     probingPublicIP = true;
    328.                     testStatus = "Testing if blocked public IP can be circumvented";
    329.                     timer = Time.time + 10;
    330.                 }
    331.                 // NAT punchthrough test was performed but we still get blocked
    332.                 else if (Time.time > timer) {
    333.                     probingPublicIP = false;         // reset
    334.                     useNat = true;
    335.                     doneTesting = true;
    336.                 }
    337.                 break;
    338.             case ConnectionTesterStatus.PublicIPNoServerStarted:
    339.                 testMessage = "Public IP address but server not initialized, "+
    340.                     "it must be started to check server accessibility. Restart "+
    341.                     "connection test when ready.";
    342.                 break;
    343.                            
    344.             case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
    345.                 testMessage = "Limited NAT punchthrough capabilities. Cannot "+
    346.                     "connect to all types of NAT servers. Running a server "+
    347.                     "is ill advised as not everyone can connect.";
    348.                 useNat = true;
    349.                 doneTesting = true;
    350.                 break;
    351.                
    352.             case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
    353.                 testMessage = "Limited NAT punchthrough capabilities. Cannot "+
    354.                     "connect to all types of NAT servers. Running a server "+
    355.                     "is ill advised as not everyone can connect.";
    356.                 useNat = true;
    357.                 doneTesting = true;
    358.                 break;
    359.            
    360.             case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
    361.             case ConnectionTesterStatus.NATpunchthroughFullCone:
    362.                 testMessage = "NAT punchthrough capable. Can connect to all "+
    363.                     "servers and receive connections from all clients. Enabling "+
    364.                     "NAT punchthrough functionality.";
    365.                 useNat = true;
    366.                 doneTesting = true;
    367.                 break;
    368.    
    369.             default:
    370.                 testMessage = "Error in test routine, got " + connectionTestResult;
    371.         }
    372.         if (doneTesting) {
    373.             if (useNat)
    374.                 shouldEnableNatMessage = "When starting a server the NAT "+
    375.                     "punchthrough feature should be enabled (useNat parameter)";
    376.             else
    377.                 shouldEnableNatMessage = "NAT punchthrough not needed";
    378.             testStatus = "Done testing";
    379.         }
    380.     }
    381.  
    382.  
    383.  
    384.  
    385.  
    386.  
    387.  
    388.  
    389.  
    390.  
    391.  
    392.  

    and this is the menu

    Code (JavaScript):
    1. ///////////////////////////////////connection menu script
    2.  
    3. private var menu :String;
    4. /*
    5.     Menus
    6.     1- main_menu
    7.     2- host_menu
    8.     3-join_menu
    9.    
    10.     prefs
    11.     1-MainPlayerName
    12. */
    13. public var playerName : String;   //// the name of the player
    14. public var battleName : String ; ///////////////////////name of the round
    15. public var playersLimit : int;   //// the max number of players in a round
    16. private var  serverManager : ConnectionScript;
    17.  
    18.  
    19. //////////////////////////////////////////php new
    20. public var gameType : String;
    21. public var gameName : String;
    22. public var comment :String = "";
    23. public var updatesDelay : float = 10.0;
    24. //////////////////////////////////////////level loading
    25. var supportedNetworkLevel : String[];
    26. var disconnectedLevel : String;
    27. private var lastLevelPrefix = 0;
    28.  
    29. //////////////////////////////////////////level loading
    30.  
    31.  
    32. function Awake() {
    33.         GetComponent.<NetworkView>().group = 1;
    34.         serverManager = this.GetComponent.<ConnectionScript>();
    35. }
    36. function Start(){
    37.     menu = "main_menu";
    38.     playerName = PlayerPrefs.GetString("MainPlayerName");
    39. }
    40.  
    41.  
    42. function moveToMenu(menuName : String){
    43.     menu = menuName;
    44. }
    45.  
    46. function OnGUI(){
    47.     if(menu == "main_menu"){
    48.         mainMenu();
    49.     }
    50.     if(menu == "host_menu"){
    51.         hostMenu();
    52.     }
    53.     if(menu == "lobby_menu"){
    54.         lobbyMenu();
    55.     }
    56.     if(menu == "list_menu"){
    57.         listMenu();
    58.     }
    59. }
    60.  
    61. private function mainMenu(){
    62.     if(GUI.Button(new Rect(0,0,120,32),"Crear Sala")){
    63.         moveToMenu("host_menu");
    64.     }
    65.     playerName = GUI.TextField(new Rect(0,35,120,32),playerName);
    66.     if(GUI.Button(new Rect(0,73,120,32),"Guardar Nombre")){
    67.         PlayerPrefs.SetString("MainPlayerName",playerName);
    68.     }
    69.    
    70.     if(GUI.Button(new Rect(125,0,120,32),"Unirse a sala")){
    71.         serverManager.getServers(gameType);
    72.         moveToMenu("list_menu");
    73.     }  
    74. }
    75.  
    76. private function hostMenu(){
    77.     if(GUI.Button(new Rect(225,0,120,32),"Iniciar")){
    78.         ConnectionScript.cManager.initiateServer("",gameType, gameName,playersLimit);
    79.         moveToMenu("lobby_menu");
    80.     }
    81.    
    82.     gameName = GUI.TextField(new Rect(0,0,120,32), gameName);
    83.     if (GUI.Button(new Rect(125, 0, 30, 30), "-") && playersLimit > 0) {
    84.         playersLimit--;
    85.     }
    86.     if (GUI.Button(new Rect(185, 0, 30, 30), "+") && playersLimit < 16) {
    87.         playersLimit++;
    88.     }
    89.     GUI.Label(new Rect(155,0,30,30),playersLimit.ToString());
    90.        
    91.    
    92.     if(GUI.Button(new Rect(0,33,120,32),"Volver")){
    93.         moveToMenu("main_menu");
    94.     }  
    95. }
    96.  
    97. function listMenu(){
    98.         if(GUI.Button(new Rect(0,0,120,32),"Refrescar")){
    99.             serverManager.getServers(gameType);
    100.             serverManager.PollHostList();
    101.         }
    102.         if (GUI.Button(new Rect(125, 0, 120, 23), "Volver")) {
    103.             moveToMenu("main_menu");      
    104.         }
    105.        
    106.     GUILayout.BeginArea(new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height), "Lista de servers", "box");
    107.    
    108.     var hostDataList : HostData[] = serverManager.PollHostList();
    109.    
    110.     if(hostDataList != null && hostDataList.length >0 ){
    111.     for (var hostData  in hostDataList ) {  
    112.         GUILayout.BeginHorizontal();
    113.         GUILayout.Label(hostData.gameName);
    114.         if (GUILayout.Button("Entrar"))
    115.             {
    116.                 Network.Connect(hostData.guid);
    117.                 moveToMenu("lobby_menu");
    118.             }
    119.         GUILayout.EndHorizontal();      
    120.         }
    121.     }
    122.     GUILayout.EndArea();
    123. }
    124.  
    125. function lobbyMenu(){
    126.     if(Network.isServer){
    127.         if (GUI.Button(new Rect(0, 0, 120, 30), "Iniciar juego")) {  
    128.             ConnectionScript.cManager.GetComponent.<NetworkView>().RPC("startGame", RPCMode.AllBuffered, supportedNetworkLevel[0], 1);          
    129.         }
    130.     var curIPAdrres = Network.player.externalIP;
    131.     var curPort = Network.player.externalPort;
    132.     GUI.Label(new Rect(0,90,500,20),"IP: "+curIPAdrres+" Port: "+curPort);
    133.     }
    134.     if (GUI.Button(new Rect(0, 35, 120, 25), "Salir")) {
    135.         serverManager.DisconnectNow();
    136.         moveToMenu("main_menu");
    137.     }
    138. }
    139.  
    140.  
    141.  
    142.  
    143.  

    and when i change the the connection function to

    Code (JavaScript):
    1. Network.Connect(hostData.guid);
    i receive this error NAT target 2147483647 not connected to NAT facilitator 67.225.180.24:5000