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

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:

using System;
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Text;
using UnityEngine.Networking;

public class NetManager : NetworkManager
{
    public string ServerName = "Noname DW Server";
    public bool NotifyMasterServer = true;

    protected string serverhash;

    public override void OnStartServer()
    {
        if (NotifyMasterServer == false || networkAddress.Equals("localhost") || networkAddress.Equals("127.0.0.1")) return;
        try
        {
            TcpClient _client = new TcpClient(Constants.MSIP, Constants.MSPort);
            NetworkStream stream = _client.GetStream();
            StringBuilder builder = new StringBuilder();
 
            string command = ServerName + Indexes.splitter + networkAddress + Indexes.splitter + networkPort + Indexes.add + Indexes.end;
            
            byte[] data = Encoding.UTF8.GetBytes(command);
            stream.Write(data,0,data.Length);
            
            data = new byte[128];
            do
            {
                stream.Read(data, 0, data.Length);
                builder.Append(Encoding.UTF8.GetString(data));
            } while (stream.DataAvailable);
            
            command = builder.ToString().Split(new string[] {Indexes.end}, StringSplitOptions.None)[0];
            
            if (command.Contains(Indexes.aresp))
            {
                command = command.Split(new string[] {Indexes.splitter}, StringSplitOptions.None)[0];
                serverhash = command;
            }
            _client.Close();

            _client = new TcpClient(Constants.MSIP, Constants.MSPort);
            stream = _client.GetStream();
        }
        catch (Exception ex)
        {
            if (ex.GetType() == typeof(SocketException)) Debug.LogError("Failed to connect to master server");
        }
        base.OnStartServer();
    }
}

maybe something like :

 /// <summary>
    /// Load map hook;
    /// </summary>
    /// <param name="sceneName"></param>
    public override void OnServerSceneChanged(string sceneName)
    {
        if (sceneName == "scene")
        {
            ServerDataControl.LoadServer(); // Load method here;
        }
    }

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.

This interesting idea, i shall try.

EDIT:
Wooops, i have logical error in:

if(|| networkAddress.Equals("localhost")|| networkAddress.Equals("127.0.0.1")) return;
  • i use for testing 127.0.0.1 , LOL. Thanks!
1 Like