Legacy Documentation: Version 5.1
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

Sumbission failed

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

Close

Cancel

Switch to Manual
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.

import Networking;

function Start() { NetworkServer.Instance.Listen(7070); Debug.Log ("Registering server callbacks"); NetworkServer.Instance.RegisterHandler(MsgType.SYSTEM_CONNECT, OnConnected); }

function OnConnected(conn : NetworkConnection, reader : NetworkReader) { Debug.Log ("Client connected"); }

The system message types are listed below:

public class MsgType
{
	static public short SYSTEM_CONNECT = 1;
	static public short SYSTEM_DISCONNECT = 2;
	static public short SYSTEM_OBJ_CREATE = 3;
	static public short SYSTEM_OBJ_DESTROY = 4;
    static public short SYSTEM_ERROR = 5;		
	static public short SYSTEM_READY = 7;
	static public short SYSTEM_RPC = 8;
	static public short SYSTEM_OBJ_SPAWN = 9;
	static public short SYSTEM_UPDATE = 10;
	static public short SYSTEM_OWNER = 11;
	static public short SYSTEM_COMMAND = 12;
    static public short SYSTEM_COMMAND_SLOW = 13;
    static public short SYSTEM_RPC_SLOW = 14;
}

Most of these messages are for internal use only. So users should not define message ids in this range. (NOTE: this should be enforced?)