WebSocket API 文档
WebSocket API 为 EasyBot 插件提供了完整的 WebSocket 客户端和服务端功能,支持实时双向通信。
WebSocket 客户端
CreateClient()
功能: 创建 WebSocket 客户端
语法: CreateClient(url: string, id?: string): string
参数:
url(string): WebSocket 服务器地址id(string, 可选): 客户端ID,为空则自动生成
返回值: string - 客户端ID
示例:
const clientId = ws.CreateClient("ws://localhost:8080", "my-client");
logger.info("客户端ID:", clientId);
ConnectAsync()
功能: 连接 WebSocket 服务器
语法: ConnectAsync(id: string): Promise<boolean>
参数:
id(string): 客户端ID
返回值: Promise<boolean> - 是否连接成功
示例:
const connected = await ws.ConnectAsync("my-client");
if (connected) {
logger.info("连接成功");
}
DisconnectAsync()
功能: 断开 WebSocket 连接
语法: DisconnectAsync(id: string): Promise<boolean>
参数:
id(string): 客户端ID
返回值: Promise<boolean> - 是否断开成功
示例:
const disconnected = await ws.DisconnectAsync("my-client");
SendTextAsync()
功能: 发送文本消息
语法: SendTextAsync(id: string, message: string): Promise<boolean>
参数:
id(string): 客户端IDmessage(string): 消息内容
返回值: Promise<boolean> - 是否发送成功
示例:
const sent = await ws.SendTextAsync("my-client", "Hello Server!");
SendBinaryAsync()
功能: 发送二进制消息
语法: SendBinaryAsync(id: string, data: Uint8Array): Promise<boolean>
参数:
id(string): 客户端IDdata(Uint8Array): 二进制数据
返回值: Promise<boolean> - 是否发送成功
示例:
const binaryData = new Uint8Array([1, 2, 3, 4]);
const sent = await ws.SendBinaryAsync("my-client", binaryData);
SetMessageCallback()
功能: 设置消息接收回调
语法: SetMessageCallback(id: string, callback: (message: string, data: Uint8Array | null) => void): boolean
参数:
id(string): 客户端IDcallback(function): 回调函数
返回值: boolean - 是否设置成功
示例:
ws.SetMessageCallback("my-client", (message, data) => {
if (message) {
logger.info("收到文本消息:", message);
}
if (data) {
logger.info("收到二进制数据:", data);
}
});
SetStateCallback()
功能: 设置连接状态变化回调
语法: SetStateCallback(id: string, callback: (state: number) => void): boolean
参数:
id(string): 客户端IDcallback(function): 回调函数
返回值: boolean - 是否设置成功
示例:
ws.SetStateCallback("my-client", (state) => {
logger.info("连接状态变化:", state);
});
GetClientState()
功能: 获取客户端状态
语法: GetClientState(id: string): number
参数:
id(string): 客户端ID
返回值: number - 客户端状态
示例:
const state = ws.GetClientState("my-client");
logger.info("当前状态:", state);
DestroyClientAsync()
功能: 销毁客户端
语法: DestroyClientAsync(id: string): Promise<boolean>
参数:
id(string): 客户端ID
返回值: Promise<boolean> - 是否销毁成功
示例:
const destroyed = await ws.DestroyClientAsync("my-client");
GetClientIds()
功能: 获取所有客户端ID
语法: GetClientIds(): string[]
返回值: string[] - 客户端ID列表
示例:
const clientIds = ws.GetClientIds();
logger.info("所有客户端:", clientIds);
WebSocket 服务端
CreateServer()
功能: 创建 WebSocket 服务器
语法: CreateServer(port: number, id?: string, host?: string, path?: string): string
参数:
port(number): 监听端口id(string, 可选): 服务器ID,为空则自动生成host(string, 可选): 监听地址,默认为 localhostpath(string, 可选): 监听路径,默认为 "/"
返回值: string - 服务器ID
示例:
const serverId = ws.CreateServer(8080, "my-server", "localhost", "/ws");
StartServerAsync()
功能: 启动 WebSocket 服务器
语法: StartServerAsync(id: string): Promise<boolean>
参数:
id(string): 服务器ID
返回值: Promise<boolean> - 是否启动成功
示例:
const started = await ws.StartServerAsync("my-server");
StopServerAsync()
功能: 停止 WebSocket 服务器
语法: StopServerAsync(id: string): Promise<boolean>
参数:
id(string): 服务器ID
返回值: Promise<boolean> - 是否停止成功
示例:
const stopped = await ws.StopServerAsync("my-server");