Version: 5.6 (switch to 2017.1b)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

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 function RegisterHandler(msgType: short, handler: Networking.NetworkMessageDelegate): void;
public static void RegisterHandler(short msgType, Networking.NetworkMessageDelegate handler);

Parameters

msgType Message type number.
handler Function 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.

#pragma strict
class MyServer {
    function Start() {
        NetworkServer.Listen(7070);
        Debug.Log("Registering server callbacks");
        NetworkServer.RegisterHandler(MsgType.Connect, OnConnected);
    }
    function OnConnected(netMsg: NetworkMessage) {
        Debug.Log("Client connected");
    }
}
class MyServer
{
    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:

#pragma strict
class MsgType {
    public const var ObjectDestroy: short = 1;
    public const var Rpc: short = 2;
    public const var ObjectSpawn: short = 3;
    public const var Owner: short = 4;
    public const var Command: short = 5;
    public const var LocalPlayerTransform: short = 6;
    public const var SyncEvent: short = 7;
    public const var UpdateVars: short = 8;
    public const var SyncList: short = 9;
    public const var ObjectSpawnScene: short = 10;
    public const var NetworkInfo: short = 11;
    public const var SpawnFinished: short = 12;
    public const var ObjectHide: short = 13;
    public const var CRC: short = 14;
    public const var LocalClientAuthority: short = 15;
}
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.