版本: Unity 6 (6000.0)
语言英语
  • C#

ChannelClient

UnityEditor.MPE 中的类

建议更改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们会阅读用户提出的每一项更改建议,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的更改建议无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

描述

ChannelClient 是一个 WebSocket 客户端,它连接到 Unity 的 ChannelService,后者是一个 WebSocket 服务器。

ChannelClient 在特定通道上创建。通道名称与 WebSocket URL 的最后一部分匹配。例如:127.0.0.1:9090/<channelName>。

EventService 是连接到“events”通道的 ChannelClient。

您可以使用 ChannelService 创建自定义通道,并使用 ChannelClient 连接到它们。有关创建通道的信息,请参阅 ChannelService.GetOrCreateChannel

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) { // A new message is received. // Since our clients 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}");

// Client has sent a message (this is a ping) // Lets 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

*/

属性

channelName此 ChannelClient 连接到的通道的名称。该名称与用于连接到 Unity 的 ChannelService 的 URL 的路由匹配。例如,127.0.0.1:8928/<my Channel Name>。
clientId通道 ID,本质上是通道名称的哈希值。请参阅 ChannelService.ChannelNameToId。
isAutoTick指定 Unity 是否自动处理(滴答)此 ChannelClient 的传入和传出消息,或者用户在主线程或专用线程中手动处理(滴答)它们。

公共方法

关闭关闭 ChannelClient。这将关闭 WebSocket 客户端,但不会关闭 ChannelService 中的 Channel。其他 ChannelClient 仍然可以连接到同一 Channel。
GetChannelClientInfo获取特定通道的 ChannelClientInfo。
IsConnected检查 ChannelClient 是否已连接到 ChannelService。
NewRequestId在此 Unity 实例中为此 ChannelClient 创建唯一的请求 ID。有关请求的更多信息,请参阅 ChannelClient.Request。
RegisterMessageHandler在特定通道上注册新的处理程序。每当向 ChannelClient 发送消息时,都会调用新的处理程序。
Send向 ChannelService 发送 ASCII 或二进制消息。根据通道的处理程序如何处理消息,它也可能发送到其他连接。
Start启动现有的 ChannelClient,使其侦听传入和传出消息。
Stop停止特定 ChannelClient 侦听新消息。这与 ChannelClient.Close 不同,因为您可以使用 ChannelClient.Start 重新启动通道客户端。
Tick滴答 ChannelClient。当您调用此方法时,它会检查服务器是否有任何传入消息需要处理,以及是否有任何传出消息需要发送到服务器。
UnregisterMessageHandler从 ChannelClient 注销特定通道处理程序。

静态方法

GetChannelClientList获取在单个 Unity 实例上运行的所有 ChannelClient 的信息。
GetOrCreateClient在特定通道上创建一个新的 ChannelClient。如果客户端已存在,则此方法获取该客户端。
Shutdown关闭此 Unity 实例中的所有 ChannelClient。