Configuring an Extended Command

Configuring an Extended Command

How to Register/use the Extended Command Type

The following shows how to implement your own extended command message and handler. The main components are:

  1. Register your command handler to NetworkManager

  2. Implement the handler and process your payload

Example Implementation:

// Requirements: // Register your command handler NetworkManager.Instance.RegisterExtendedCommandHandler("[Type.NameHere]", ProcessExtendedCommandHandlerHere); // Implement your handlers processor `ProcessExtendedCommandHandlerHere` private void ProcessExtendedCommandHandlerHere(ushort connectionId, uint fromNetworkId, uint networkId, UserWorldContext ux, string type, byte[] commandBuffer) { /* Process message here */ } // Example: // Register to NetworkManager NetworkManager.Instance.RegisterExtendedCommandHandler("Diagnostic.timeToJoin", ProcessExtendedTimeToJoin); // Build message and fire off // A ProcessUserJoin starts and ends, invoking UserJoinedInXSeconds via an event handler which prepares the message and sends it. private void UserJoinedInXSeconds(object sender, long timeTaken) { // Declare type and payload var type = "Diagnostic.timeToJoin"; var message = $"User joined the session in: {timeTaken} ms."; // Encode payload byte[] buffer = Encoding.UTF8.GetBytes(message); // Pass onto NetworkManager to broadcast message to clients NetworkManager.Instance.SendToHostOrBroadcast(new ExtendedCommandMessage { Command = buffer, ExtendedType = type }); } // On client's receiving extended command, process payload. private void ProcessExtendedTimeToJoin(ushort connectionId, uint fromNetworkId, uint networkId, UserWorldContext ux, string type, byte[] commandBuffer) { // Decode the command buffer back into the original string message string message = Encoding.UTF8.GetString(commandBuffer); UnityEngine.Debug.Log($"Received extended command '{type}' from NetworkId: {fromNetworkId}. Message: {message}"); }