- Home /
Question by
awesomeface · Sep 18, 2014 at 07:58 AM ·
javascriptnetworkingnetworkconnect
Connection to a registered host not working
#pragma downcast
var serverPort : int = 20000;
var numberOfPlayers : int = 3;
var numberOfConnections : int;
var gameName : String = "Game Name";
function Awake() {
MasterServer.ClearHostList();
}
function Update () {
}
function OnGUI () {
if(Network.peerType == NetworkPeerType.Disconnected){
if(GUI.Button(Rect(0,90,100,30),"Refresh")){
MasterServer.RequestHostList("GameTest");
}
serverPort = parseInt(GUI.TextField(Rect(0,30,100,30),serverPort.ToString()));
numberOfPlayers = parseInt(GUI.TextField(Rect(0,60,100,30),numberOfPlayers.ToString()));
gameName = GUI.TextField(Rect(0,0,100,30),gameName);
if(gameName != ""){
if(GUI.Button(Rect(0,120,100,30),"Create Server")){
Network.InitializeServer(numberOfPlayers,serverPort,!Network.HavePublicAddress());
MasterServer.RegisterHost("GameTest",gameName);
}
}
if (MasterServer.PollHostList().Length != 0) {
var i : int = 0;
var hostData: HostData[] = MasterServer.PollHostList();
while(i < hostData.Length){
GUI.Label(Rect(100,i*20,100,20),hostData[i].gameName);
GUI.Label(Rect(200,i*20,50,20),"[" + hostData[i].connectedPlayers.ToString() + "/" + hostData[i].playerLimit.ToString() + "]");
if(GUI.Button(Rect(250,i*20,100,20),"Connect")){
Network.Connect(hostData[i].ip,hostData[i].port);
}
i += 1;
}
}
}
if(Network.isServer){
numberOfConnections = Network.connections.Length;
GUI.Label(Rect(0,0,100,30),numberOfConnections.ToString());
}
}
function OnPlayerDisconnected(player: NetworkPlayer) {
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
function OnDisconnectedFromServer() {
Application.LoadLevel(0);
}
The above code is from a script I titled "Menu", what I'm trying to accomplish is a simple server connect and create over the internet and whenever I try connecting to a server over the internet, the console gives me:
Connected to master server at 67.225.180.24:23466
Incoming host list query response from master server.
Running as client. No player ID set yet.
UnityEngine.Network:Connect(String[], Int32) Menu:OnGUI() (at Assets/Menu.js:34)
But that's normal as when I connect over Lan it gives me that and even more, telling me all of the information about the server, but when I try to connect over the internet, it gives me (in addition to the ones above):
The connection request to 124.187.118.95:60751 failed. Are you sure the server can be connected to?
What am I missing? Am I going about networking incorrectly? Someone help me please
Comment
Answer by basketballgames · Jul 23, 2019 at 03:34 AM
Thank you for the information. Great job, @.awesomeface .. ! atari breakout