Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[uNet] How to perform actions at the start of the server?

Discussion in 'UNet' started by SovietGeneral, Nov 8, 2016.

  1. SovietGeneral

    SovietGeneral

    Joined:
    Jul 31, 2016
    Posts:
    41
    I'm writing my master server. Faced with a problem: the need to inform the master server about it, and send it. The main problem: OnStartServer() seems not called when server starts and hence the code described in it, is not executed. The class that's responsible inherited from NetworkManager and parareality method.
    That's the problem.

    A more specific question - what is called when you create the server and where where it should hang. Please tell me.

    if you are interested, here is the class:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Net.Sockets;
    5. using System.Text;
    6. using UnityEngine.Networking;
    7.  
    8. public class NetManager : NetworkManager
    9. {
    10.     public string ServerName = "Noname DW Server";
    11.     public bool NotifyMasterServer = true;
    12.  
    13.     protected string serverhash;
    14.  
    15.     public override void OnStartServer()
    16.     {
    17.         if (NotifyMasterServer == false || networkAddress.Equals("localhost") || networkAddress.Equals("127.0.0.1")) return;
    18.         try
    19.         {
    20.             TcpClient _client = new TcpClient(Constants.MSIP, Constants.MSPort);
    21.             NetworkStream stream = _client.GetStream();
    22.             StringBuilder builder = new StringBuilder();
    23.  
    24.             string command = ServerName + Indexes.splitter + networkAddress + Indexes.splitter + networkPort + Indexes.add + Indexes.end;
    25.            
    26.             byte[] data = Encoding.UTF8.GetBytes(command);
    27.             stream.Write(data,0,data.Length);
    28.            
    29.             data = new byte[128];
    30.             do
    31.             {
    32.                 stream.Read(data, 0, data.Length);
    33.                 builder.Append(Encoding.UTF8.GetString(data));
    34.             } while (stream.DataAvailable);
    35.            
    36.             command = builder.ToString().Split(new string[] {Indexes.end}, StringSplitOptions.None)[0];
    37.            
    38.             if (command.Contains(Indexes.aresp))
    39.             {
    40.                 command = command.Split(new string[] {Indexes.splitter}, StringSplitOptions.None)[0];
    41.                 serverhash = command;
    42.             }
    43.             _client.Close();
    44.  
    45.             _client = new TcpClient(Constants.MSIP, Constants.MSPort);
    46.             stream = _client.GetStream();
    47.         }
    48.         catch (Exception ex)
    49.         {
    50.             if (ex.GetType() == typeof(SocketException)) Debug.LogError("Failed to connect to master server");
    51.         }
    52.         base.OnStartServer();
    53.     }
    54. }
    55.  
     
  2. Deleted User

    Deleted User

    Guest

    maybe something like :
    Code (CSharp):
    1.  /// <summary>
    2.     /// Load map hook;
    3.     /// </summary>
    4.     /// <param name="sceneName"></param>
    5.     public override void OnServerSceneChanged(string sceneName)
    6.     {
    7.         if (sceneName == "scene")
    8.         {
    9.             ServerDataControl.LoadServer(); // Load method here;
    10.         }
    11.     }
    It helps me to load my server map, OnServerChanged is called when Server is setted up and started, also NetworkServer.active is true. So this means that server is ready for send a message to Master Server about RegisterHost.
     
  3. SovietGeneral

    SovietGeneral

    Joined:
    Jul 31, 2016
    Posts:
    41
    This interesting idea, i shall try.

    EDIT:
    Wooops, i have logical error in:
    Code (CSharp):
    1. if(|| networkAddress.Equals("localhost")|| networkAddress.Equals("127.0.0.1")) return;
    - i use for testing 127.0.0.1 , LOL. Thanks!
     
    Last edited: Nov 10, 2016
    Deleted User likes this.