Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 diekeure · Jul 18, 2011 at 06:30 PM · networkslow

Network setup

Hello, I browsed all relevant network issue related posts, but I could not find an answer.

I build a custom master server, build with vs2008, without touching the code. I run it on my pc.

I have a unity exe that starts as a server with following c# code:

 public class ServerController : MonoBehaviour 
 {
 void Start()
 {
     DebugConsole.IsOpen = true;
     DebugConsole.Log("Server started");
 }

 void OnGUI()
 {
     if (GUILayout.Button("Start Server"))
     {
         Network.InitializeServer(32, 23467, false);
         MasterServer.ipAddress = "127.0.0.1";
         MasterServer.RegisterHost("GameName", "ZeGame", "l33t game for all");
     }
 }

 void OnPlayerConnected(NetworkPlayer player)
 {
     DebugConsole.Log("Player " + player + " connected from " + player.ipAddress + ":" + player.port);
 }

 void OnServerInitialized()
 {
     DebugConsole.Log("Server is initialized");
 }

 void OnPlayerDisconnected(NetworkPlayer player)
 {
     Debug.Log("Clean up after player " + player);
     Network.RemoveRPCs(player);
     Network.DestroyPlayerObjects(player);
 }
 }

And a client that runs following script:

 public class ClientController : MonoBehaviour 
 {
 void Start()
 {
     MasterServer.ipAddress = "127.0.0.1";
     MasterServer.RequestHostList("WimiAmericanRoulette");

     DebugConsole.IsOpen = true;
     DebugConsole.Log("Client started");
 }

 void OnConnectedToServer() 
 {
     DebugConsole.Log("Connected to server");
     DebugConsole.Log("network: " + Network.peerType);
 }

 void OnDisconnectedFromServer(NetworkDisconnection info)
 {
     if (info == NetworkDisconnection.LostConnection)
         DebugConsole.Log("Lost connection to the server");
     else
         DebugConsole.Log("Successfully disconnected from the server");
 }

 void OnFailedToConnect(NetworkConnectionError error)
 {
     DebugConsole.Log("Could not connect to server: " + error);
 }

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

 void OnGUI() 
 {
     if (GUILayout.Button("Refresh"))
     {
         MasterServer.ClearHostList();
         MasterServer.RequestHostList("WimiAmericanRoulette");
     }
     HostData[] data = MasterServer.PollHostList();
     // Go through all the hosts in the host list
     foreach (HostData element in data)
     {
         GUILayout.BeginHorizontal();    
         string name = element.gameName + " " + element.connectedPlayers + " / " + element.playerLimit;
         GUILayout.Label(name);    
         GUILayout.Space(5);
         string hostInfo;
         hostInfo = "[";
         foreach (string host in element.ip)
             hostInfo = hostInfo + host + ":" + element.port + " ";
         hostInfo = hostInfo + "]";
         GUILayout.Label(hostInfo);    
         GUILayout.Space(5);
         GUILayout.Label(element.comment);
         GUILayout.Space(5);
         GUILayout.FlexibleSpace();
         if (GUILayout.Button("Connect"))
         {
             // Connect to HostData struct, internally the correct method is used (GUID when using NAT).

             DebugConsole.Log("Connecting..." + hostInfo);
             NetworkConnectionError error = Network.Connect(element);
             if (error != NetworkConnectionError.NoError)
             {
                 DebugConsole.Log("Failed to connect " + error);
             }
             else
             {
                 DebugConsole.Log("network: " + Network.peerType);
             }
         }
         if (GUILayout.Button("Disconnect"))
         {
             // Connect to HostData struct, internally the correct method is used (GUID when using NAT).
             Network.Disconnect();
             DebugConsole.Log("network: " + Network.peerType);
         }

         GUILayout.EndHorizontal();    
     }

     if (GUILayout.Button("Send \"Hello World\""))
     {
         networkView.RPC("RPCTest", RPCMode.Others, "Hello world");
     }
 }
 }

I can start the server, OnInitialized gets called.

Then I start the client, I receive the game from the masterserver that is running on the server. I do the Network.Connect call: it immediatly returns, in state 'disconnected' and then it takes an arbitrary time to connect. Sometimes it's round 20 seconds, other times it takes minutes. I have the impression that it helps to switch focus between server and client.

I would expect that having focus helps, but in an earlier version I could connect in a split second without having to switch focus, so that makes me think I'm doing something wrong.

And them when I disconnect, the OnPlayerDisconnected call is naver made on the server. Again, in an earlier version this was triggered in a split second.

I'm unable to find what I broke, so I hope that somebody can tell me what is wrong with the code. It is all taken from the samples.

I probably must mention that these scripts are attached to the maincamera of an empty scene (each a different scene) with a networkview also attached to the maincamera. (when connected, the position of the camera gets synced, as expected).

Many hanks for any help i can get, Alex

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jul 18, 2011 at 08:23 PM

I guess you did the common mistake and forgot to tick the "Run In Background" option ;)

edit-->Project Settings-->Player

Comment
Add comment · Show 1 · Share
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
avatar image diekeure · Jul 19, 2011 at 07:02 AM 0
Share

Indeed i did :) Thanks, how this slipped my $$anonymous$$d I don't know.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Connecting with client causes n+1 players to spawn 0 Answers

MultiPlayer - NetworkPlayer Event 2 Answers

Super slow network speed in editor with some routers 0 Answers

sync video on all network clients 1 Answer

Multiplayer FPS Controls Both 0 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