Legacy Documentation: Version 2018.2 (Go to current version)
LanguageEnglish
  • C#

NetworkServer.RegisterHandler

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static void RegisterHandler(short msgType, Networking.NetworkMessageDelegate handler);

Parameters

msgTypeMessage type number.
handlerFunction handler which will be invoked for when this message type is received.

Description

Register a handler for a particular message type.

There are several system message types which you can add handlers for. You can also add your own message types.

using UnityEngine;
using UnityEngine.Networking;

public class MyServer : NetworkManager { void Start() { NetworkServer.Listen(7070); Debug.Log("Registering server callbacks"); NetworkServer.RegisterHandler(MsgType.Connect, OnConnected); }

void OnConnected(NetworkMessage netMsg) { Debug.Log("Client connected"); } }

The system message types are listed below:

class MsgType
{
    public const short ObjectDestroy = 1;
    public const short Rpc = 2;
    public const short ObjectSpawn = 3;
    public const short Owner = 4;
    public const short Command = 5;
    public const short LocalPlayerTransform = 6;
    public const short SyncEvent = 7;
    public const short UpdateVars = 8;
    public const short SyncList = 9;
    public const short ObjectSpawnScene = 10;
    public const short NetworkInfo = 11;
    public const short SpawnFinished = 12;
    public const short ObjectHide = 13;
    public const short CRC = 14;
    public const short LocalClientAuthority = 15;
}

Most of these messages are for internal use only. Users should not define message ids in this range.

Did you find this page useful? Please give it a rating: