Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kezmacgov · Jan 12, 2011 at 10:07 AM · networking

masterserver connection

i have created this code but do not no i f i am connecet to the master server or not. could someone have a look and let me no if its wrong or not. i can connect locally but i recieve no acknowledgement i am connected to the master server. please help

/* * This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl) * The original author of this code Mike Hergaarden, even though some small parts * are copied from the Unity tutorials/manuals. * Feel free to use this code for your own projects, drop me a line if you made something exciting! */ #pragma strict

var connectToIP : String = "127.0.0.1"; var connectPort : int = 25002; private var entrypassword: String =""; private var passwordID = "id"; private var password : String = ""; private var tempPassword = password; // stores temporary password private var ps =0; private var pw =0; private var sx =0; private var sy =0; private var a = false;

var doWindow0 = false;

private var connectStatus = "";

function DoWindow0 (windowID : int) { if(GUI.Button (Rect (10,30, 80,20), "Ok")) { doWindow0 = false; } }

function Awake() { // Make sure list is empty and request a new list MasterServer.ClearHostList(); MasterServer.RequestHostList("LarusTest"); }

function Update() { // If any hosts were received, display game name, the clear host list again if (MasterServer.PollHostList().length != 0) { var hostData: HostData[] = MasterServer.PollHostList(); for (var i:int=0; i<hostData.length; i++) { Debug.Log("Game name: " + hostData[i].gameName); } MasterServer.ClearHostList(); connectStatus = "in if"; doWindow0 = true; }

}

//Obviously the GUI is for both client&servers (mixed!) function OnGUI () { //if gui window pressed enter this if (doWindow0) { GUI.Window (0, Rect (110,10,200,60), DoWindow0, connectStatus); }

if(a==false) { //entry password GUILayout.Label("enter password"); entrypassword = GUILayout.PasswordField (entrypassword, "*"[0], 25); tempPassword = entrypassword;

 if(GUILayout.Button("Ok")) 
 {
     if(passwordID == tempPassword)
     {
         tempPassword = "";
         a= true;
         print("right");
         connectStatus = "Correct";
         doWindow0 = true;
     }
     else 
     {
         connectStatus = "Wrong password try again";
         doWindow0 = true;
         print("wrong");
     }
 }





} //doWindow0 = GUI.Toggle (Rect (10,10,100,20), doWindow0, "Window 0"); else if(a==true) { if (Network.peerType == NetworkPeerType.Disconnected) { //We are currently disconnected: Not a client or host GUILayout.Label("Connection status: Disconnected");

     connectToIP = GUILayout.TextField(connectToIP, GUILayout.MinWidth(100));
     connectPort = parseInt(GUILayout.TextField(connectPort.ToString()));
     //password =  GUILayout.TextField(password, GUILayout.MinWidth(100));
     password = GUILayout.PasswordField (password, "*"[0], 25);




         GUILayout.BeginVertical();
         if (GUILayout.Button ("Connect as client"))
         {
             //Connect to the "connectToIP" and "connectPort" as entered via the GUI
             //Ignore the NAT for now
             //~ if(password == tempPassword)            
             //~ {
                 connectStatus = "Checking";
                 doWindow0 = true;
                 Network.useNat = false;
                 Network.Connect(connectToIP, connectPort,password);
                 tempPassword = "";
             //~ }


         }



         if (GUILayout.Button ("Start Server"))
         {
             //Start a server for 32 clients using the "connectPort" given via the GUI
             //Ignore the nat for now    
             //Network.useNat = false;
             Network.useNat = !Network.HavePublicAddress();
             Network.incomingPassword = password;
             Network.InitializeServer(32, connectPort);

             MasterServer.RegisterHost("MyUniqueGameType", "JohnDoes game", "l33t game for all");
         }
         GUILayout.EndVertical();


 }
 else
 {
     //We've got a connection(s)!


     if (Network.peerType == NetworkPeerType.Connecting)
     {

         GUILayout.Label("Connection status: Connecting");

     }
     else if (Network.peerType == NetworkPeerType.Client)
     {

         GUILayout.Label("Connection status: Client!");
         GUILayout.Label("Ping to server: "+Network.GetAveragePing(  Network.connections[0] ) );     

     } 
     else if (Network.peerType == NetworkPeerType.Server)
     {

         GUILayout.Label("Connection status: Server!");
         GUILayout.Label("Connections: "+Network.connections.length);
         if(Network.connections.length&gt;=1)
         {
             GUILayout.Label("Ping to first player: "+Network.GetAveragePing(  Network.connections[0] ) );
         }           
     }

     if (GUILayout.Button ("Disconnect"))
     {
         Network.Disconnect(200);
     }
 }

 }

}

// NONE of the functions below is of any use in this demo, the code below is only used for demonstration. // First ensure you understand the code in the OnGUI() function above.

//Client functions called by Unity function OnConnectedToServer() { Debug.Log("This CLIENT has connected to a server"); }

function OnDisconnectedFromServer(info : NetworkDisconnection) { Debug.Log("This SERVER OR CLIENT has disconnected from a server"); }

function OnFailedToConnect(error: NetworkConnectionError) { Debug.Log("Could not connect to server: "+ error);

 connectStatus = "Error Failed";
 doWindow0 = true;

}

//Server functions called by Unity function OnPlayerConnected(player: NetworkPlayer) { Debug.Log("Player connected from: " + player.ipAddress +":" + player.port); }

function OnServerInitialized() { Debug.Log("Server initialized and ready"); }

function OnPlayerDisconnected(player: NetworkPlayer) { Debug.Log("Player disconnected from: " + player.ipAddress+":" + player.port); }

// OTHERS: // To have a full overview of all network functions called by unity // the next four have been added here too, but they can be ignored for now

function OnFailedToConnectToMasterServer(info: NetworkConnectionError) { Debug.Log("Could not connect to master server: "+ info); }

function OnNetworkInstantiate (info : NetworkMessageInfo) { Debug.Log("New object instantiated by " + info.sender); }

function OnSerializeNetworkView(stream : BitStream, info : NetworkMessageInfo) { //Custom code here (your code!) }

/* The last networking functions that unity calls are the RPC functions. As we've added "OnSerializeNetworkView", you can't forget the RPC functions that unity calls..however; those are up to you to implement.

@RPC function MyRPCKillMessage(){ //Looks like I have been killed! //Someone send an RPC resulting in this function call } */

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Android/iOS Phone as Game Pad 1 Answer

How to call a MainClass Funktion ? 1 Answer

Can you create a Stand alone server for your game? 0 Answers

Unity networking,Cant see each other move 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges