ChannelService 封装了在 Unity 中运行的 WebSocket 服务器。
ChannelService 允许您创建新的通信通道。通道与连接到 ChannelService 的 URL 路由匹配。例如,“127.0.0.1:9292/<channelName>”
ChannelService 允许自定义处理程序接收特定通道上的所有消息。
Unity 不会自动启动 ChannelService。您必须使用 ChannelService.Start 手动启动它。或者,您可以使用以下命令行开关:--ump-channel-service-on-startup。
using System; using System.Text; using UnityEditor.MPE; using UnityEditor; using UnityEngine;
public static class ChannelCommunicationDocExample { [MenuItem("ChannelDoc/Step 1")] static void StartChannelService() { if (!ChannelService.IsRunning()) { ChannelService.Start(); } Debug.Log($"[Step1] ChannelService Running: {ChannelService.GetAddress()}:{ChannelService.GetPort()}"); }
static int s_BinaryChannelId; static int s_StringChannelId; static Action s_DisconnectBinaryChannel; static Action s_DisconnectStringChannel;
[MenuItem("ChannelDoc/Step 2")] static void SetupChannelService() { if (s_DisconnectBinaryChannel == null) { s_DisconnectBinaryChannel = ChannelService.GetOrCreateChannel("custom_binary_ping_pong", HandleChannelBinaryMessage); s_BinaryChannelId = ChannelService.ChannelNameToId("custom_binary_ping_pong"); } Debug.Log($"[Step2] Setup channel_custom_binary id: {s_BinaryChannelId}");
if (s_DisconnectStringChannel == null) { s_DisconnectStringChannel = ChannelService.GetOrCreateChannel("custom_ascii_ping_pong", HandleChannelStringMessage); s_StringChannelId = ChannelService.ChannelNameToId("custom_ascii_ping_pong"); } Debug.Log($"[Step2] Setup channel_custom_ascii id: {s_StringChannelId}"); }
static void HandleChannelBinaryMessage(int connectionId, byte[] data) { var msg = ""; for (var i = 0; i < Math.Min(10, data.Length); ++i) { msg += data[i].ToString(); } Debug.Log($"Channel Handling binary from connection {connectionId} - {data.Length} bytes - {msg}");
// Client has sent a message (this is a ping) // Lets send back the same message (as a pong) ChannelService.Send(connectionId, data); }
static void HandleChannelStringMessage(int connectionId, byte[] data) { // Client has sent a message (this is a ping) // Client expects string data. Encode the data and send it back as a string:
var msgStr = Encoding.UTF8.GetString(data); Debug.Log($"Channel Handling string from connection {connectionId} - {msgStr}");
// Send back the same message (as a pong) ChannelService.Send(connectionId, msgStr); }
static ChannelClient s_BinaryClient; static Action s_DisconnectBinaryClient; static ChannelClient s_StringClient; static Action s_DisconnectStringClient; [MenuItem("ChannelDoc/Step 3")] static void SetupChannelClient() { const bool autoTick = true;
if (s_BinaryClient == null) { s_BinaryClient = ChannelClient.GetOrCreateClient("custom_binary_ping_pong"); s_BinaryClient.Start(autoTick); s_DisconnectBinaryClient = s_BinaryClient.RegisterMessageHandler(HandleClientBinaryMessage); } Debug.Log($"[Step3] Setup client for channel custom_binary_ping_pong. ClientId: {s_BinaryClient.clientId}");
if (s_StringClient == null) { s_StringClient = ChannelClient.GetOrCreateClient("custom_ascii_ping_pong"); s_StringClient.Start(autoTick); s_DisconnectStringClient = s_StringClient.RegisterMessageHandler(HandleClientStringMessage); } Debug.Log($"[Step3] Setup client for channel custom_ascii_ping_pong. ClientId: {s_StringClient.clientId}"); }
static void HandleClientBinaryMessage(byte[] data) { Debug.Log($"Receiving pong binary data: {data} for clientId: {s_BinaryClient.clientId} with channelName: {s_BinaryClient.channelName}"); }
static void HandleClientStringMessage(string data) { Debug.Log($"Receiving pong data: {data} for clientId: {s_StringClient.clientId} with channelName: {s_StringClient.channelName}"); }
[MenuItem("ChannelDoc/Step 4")] static void ClientSendMessageToServer() { Debug.Log("[Step 4]: Clients are sending data!"); s_BinaryClient.Send(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }); s_StringClient.Send("Hello world!"); }
[MenuItem("ChannelDoc/Step 5")] static void CloseClients() { Debug.Log("[Step 5]: Closing clients"); s_DisconnectBinaryClient(); s_BinaryClient.Close();
s_DisconnectStringClient(); s_StringClient.Close(); }
[MenuItem("ChannelDoc/Step 6")] static void CloseService() { Debug.Log("[Step 6]: Closing clients");
s_DisconnectBinaryChannel(); s_DisconnectStringChannel();
ChannelService.Stop(); } }
/* When you execute the menu items one after the other, Unity prints the following messages to the Console window:
[Step1] ChannelService Running: 127.0.0.1:64647
[Step2] Setup channel_custom_binary id: -1698345965
[Step2] Setup channel_custom_ascii id: -930064725
[Step3] Setup client for channel custom_binary_ping_pong. ClientId: -1698345965
[Step3] Setup client for channel custom_ascii_ping_pong. ClientId: -930064725
[Step 4]: Clients are sending data!
Channel Handling binary from connection 1 - 8 bytes - 01234567
Channel Handling string from connection 2 - Hello world!
Receiving pong binary data: System.Byte[] for clientId: -1698345965 with channelName: custom_binary_ping_pong
Receiving pong data: Hello world! for clientId: -930064725 with channelName: custom_ascii_ping_pong
[Step 5]: Closing clients
[Step 6]: Closing clients
*/
广播 | 向特定通道的所有 ChannelClient 连接发送消息。 |
BroadcastBinary | 向特定通道的所有 ChannelClient 连接发送消息。 |
ChannelNameToId | 关闭特定通道以及与该通道的所有连接。 |
CloseChannel | 关闭特定通道以及与该通道的所有连接。 |
DispatchMessages | 分派自上次分派以来收到的任何消息。这会在每次编辑器滴答时自动发生,但此方法可用于强制在线程阻塞操作期间进行分派。 |
GetAddress | 获取 ChannelService 的地址。这始终是本地地址。例如,127.0.0.1。 |
GetChannelClientList | 获取连接到 ChannelService 的所有通道客户端列表。 |
GetChannelList | 获取 ChannelService 中打开的通道列表。默认情况下,ChannelService 始终具有“status”通道和“events”通道。 |
GetOrCreateChannel | 获取或创建新通道。 |
GetPort | 检索 ChannelService 运行的端口。此端口在 ChannelService 首次启动时随机选择。或者,您可以使用命令行中的 --ump-channel-service-port <portNumber> 开关指定端口。 |
IsRunning | 检查 ChannelService 是否正在运行并侦听新连接。 |
RegisterMessageHandler | 注册处理程序以处理特定通道上的所有传入消息。 |
Send | 向特定连接发送消息。消息可以是二进制或 UTF8。 |
Start | 启动 ChannelService。启动 ChannelService 后,它会侦听以下 URL 的连接:<ChannelService 地址>:<ChannelService 端口>/<通道名称>,例如,127.0.0.1:9976/events。请参阅 ChannelService.GetAddress 和 ChannnelService.GetPort。 |
Stop | 停止 ChannelService 侦听连接,并关闭任何已建立的连接。 |
UnregisterMessageHandler | 从 Channel 中注销特定处理程序。 |