API 参考
VmosEdgeClient 类
这是 SDK 的核心类,用于创建和管理设备连接。
静态方法
isWebCodecsSupported()
检测当前环境是否支持 SDK 使用。
typescript
VmosEdgeClient.isWebCodecsSupported(): boolean返回值: boolean,true 表示当前环境支持 SDK 使用,false 表示不支持。
说明:
- 此静态方法会检测浏览器是否支持 WebCodecs API 以及是否在 file:// 或 localhost 环境下运行
- SDK 需要 WebCodecs API 进行视频解码,目前仅支持在 file:// 协议或 localhost 环境下运行
- 建议在使用 SDK 之前先调用此方法进行环境检测
示例:
typescript
import { VmosEdgeClient } from "@vmosedge/web-sdk";
// 在使用 SDK 之前检测环境支持
if (!VmosEdgeClient.isWebCodecsSupported()) {
console.error("当前环境不支持 SDK 使用");
console.error("请确保:");
console.error("1. 使用现代浏览器(Chrome 94+, Edge 94+, Safari 16.4+)");
console.error("2. 使用 file:// 协议打开 HTML 文件,或通过 localhost 访问本地 Web 服务器");
return;
}
// 环境支持,可以正常使用 SDK
const client = new VmosEdgeClient({
container: document.getElementById("container")!,
config: {
ip: "192.168.1.100",
deviceId: "EDGE418ASOC2LX9C",
ports: {
video: 9999,
touch: 9997,
},
},
});检测内容:
- WebCodecs API 是否可用(
VideoDecoder等) - 是否在 file:// 协议或 localhost 环境下运行
- 浏览器是否支持必要的 Web API
构造函数
构造函数
typescript
new VmosEdgeClient(options: VmosEdgeClientOptions)类型定义: VmosEdgeClientOptions
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
options.config | VmosEdgeClientConfig | ✅ | 设备连接配置 |
options.container | HTMLElement | ✅ | DOM 容器元素,用于显示设备画面 |
options.isGroupControl | boolean | ❌ | 是否开启群控模式,默认 false |
options.retryCount | number | ❌ | 断连重试次数,默认 0(不重试) |
options.retryInterval | number | ❌ | 重试间隔(毫秒),默认 1000 |
options.videoCodecPreference | "no-preference" | "prefer-hardware" | "prefer-software" | ❌ | 视频解码偏好,默认 "no-preference" |
options.renderPreference | "default" | "high-performance" | "low-power" | ❌ | 渲染性能偏好,默认 "default" |
options.touchMoveFrequency | number | ❌ | 触控移动事件发送频率(Hz),默认 60 |
示例:
typescript
// 局域网模式(network_mode === 'macvlan')
const client = new VmosEdgeClient({
container: document.getElementById("container")!,
config: {
ip: "192.168.1.100", // 使用设备的 ip 字段
deviceId: "EDGE418ASOC2LX9C", // 使用设备的 dbid 字段
ports: {
video: 9999, // 局域网模式固定端口
touch: 9997, // 局域网模式固定端口
},
},
retryCount: 3,
retryInterval: 1000,
videoCodecPreference: "prefer-hardware",
});typescript
// 非局域网模式
const client = new VmosEdgeClient({
container: document.getElementById("container")!,
config: {
ip: "example.com", // 使用设备的 hostip 字段
deviceId: "EDGE418ASOC2LX9C", // 使用设备的 dbid 字段
ports: {
video: 22003, // 使用设备的 tcp_port 字段
touch: 24003, // 使用设备的 tcp_control_port 字段
},
},
retryCount: 3,
retryInterval: 1000,
videoCodecPreference: "prefer-hardware",
});配置字段说明:
deviceId:对应设备信息中的dbid字段ip:局域网模式使用ip字段,非局域网模式使用hostip字段ports.video:局域网模式固定为9999,非局域网模式使用tcp_port字段ports.touch:局域网模式固定为9997,非局域网模式使用tcp_control_port字段- 网络模式判断:
network_mode === 'macvlan'表示局域网模式
方法
start()
启动连接,建立与设备的连接。
typescript
client.start(): Promise<void>返回值: Promise<void>,连接成功时 resolve,失败时 reject。
示例:
typescript
try {
await client.start();
console.log("连接成功");
} catch (error) {
console.error("连接失败:", error);
}stop()
停止连接并释放所有资源。
typescript
client.stop(): void示例:
typescript
client.stop();事件监听方法
on()
注册事件监听器。
typescript
client.on<T extends VmosEdgeClientEventName>(
event: T,
listener: VmosEdgeClientEventListener<T>
): this参数:
event: 事件名称(使用VmosEdgeClientEvents中的常量)listener: 事件处理函数
返回值: 返回 this,支持链式调用。
示例:
typescript
client.on(VmosEdgeClientEvents.STARTED, () => {
console.log("连接成功");
});
client.on(VmosEdgeClientEvents.ERROR, (error) => {
console.error("发生错误:", error);
});once()
注册一次性事件监听器(触发一次后自动移除)。
typescript
client.once<T extends VmosEdgeClientEventName>(
event: T,
listener: VmosEdgeClientEventListener<T>
): this示例:
typescript
client.once(VmosEdgeClientEvents.STARTED, () => {
console.log("首次连接成功");
});off()
移除事件监听器。
typescript
client.off<T extends VmosEdgeClientEventName>(
event: T,
listener?: VmosEdgeClientEventListener<T>
): this参数:
event: 事件名称listener: 可选,要移除的监听器函数。如果不提供,则移除该事件的所有监听器。
示例:
typescript
const handler = (error: VmosEdgeErrorEvent) => console.log(error);
// 注册监听器
client.on(VmosEdgeClientEvents.ERROR, handler);
// 移除指定监听器
client.off(VmosEdgeClientEvents.ERROR, handler);
// 移除所有 ERROR 事件的监听器
client.off(VmosEdgeClientEvents.ERROR);按键控制方法
home()
按下 Home 键。
typescript
client.home(): voidback()
按下返回键。
typescript
client.back(): voidmenu()
按下多任务/菜单键。
typescript
client.menu(): voidvolumeUp()
音量增加。
typescript
client.volumeUp(): voidvolumeDown()
音量减少。
typescript
client.volumeDown(): voidsendKeyCode()
发送自定义按键码。
typescript
client.sendKeyCode(
keyCode: AndroidKeyCode,
action: AndroidKeyEventAction,
metaState?: number
): void参数:
keyCode: Android 按键码(使用AndroidKeyCode枚举)action: 按键动作(AndroidKeyEventAction.Down或AndroidKeyEventAction.Up)metaState: 可选,元键状态(如 Shift、Ctrl 等组合键)
示例:
typescript
import { AndroidKeyCode, AndroidKeyEventAction } from "@vmosedge/web-sdk";
// 按下 Power 键
client.sendKeyCode(AndroidKeyCode.Power, AndroidKeyEventAction.Down);
client.sendKeyCode(AndroidKeyCode.Power, AndroidKeyEventAction.Up);
// 按下 Enter 键(带 Shift)
client.sendKeyCode(
AndroidKeyCode.Enter,
AndroidKeyEventAction.Down,
AndroidMetaState.ShiftOn
);clickKey()
按下并释放一个按键(快捷方法)。
typescript
client.clickKey(
keyCode: AndroidKeyCode,
metaState?: AndroidMetaState
): void示例:
typescript
client.clickKey(AndroidKeyCode.Enter);文本输入方法
sendText()
向设备发送文本。
typescript
client.sendText(text: string): void参数:
text: 要发送的文本字符串
示例:
typescript
client.sendText("Hello World!");屏幕控制方法
setRotation()
设置屏幕旋转方向。
typescript
client.setRotation(rotation: 0 | 1): void参数:
rotation:0表示竖屏,1表示横屏
注意: SDK 通常会自动根据视频流方向调整旋转,仅在特殊需求下手动调用。
示例:
typescript
// 切换到横屏
client.setRotation(1);
// 切换到竖屏
client.setRotation(0);getRotation()
获取当前旋转状态。
typescript
client.getRotation(): 0 | 1返回值: 0 表示竖屏,1 表示横屏。
群控方法
setGroupControlMode()
开启或关闭群控模式。
typescript
client.setGroupControlMode(enabled: boolean): void参数:
enabled:true开启群控,false关闭群控
示例:
typescript
client.setGroupControlMode(true);joinGroupControl()
将设备加入群控组。
typescript
client.joinGroupControl(config: VmosEdgeClientConfig[]): void参数:
config: 从设备配置数组(VmosEdgeClientConfig[])
注意:
- 需要先调用
setGroupControlMode(true)开启群控模式 - 群控模式下,从设备仅需要配置
touch端口,不需要配置video端口
示例:
typescript
client.setGroupControlMode(true);
const slaveDevices = [
{
ip: "192.168.1.101",
deviceId: "device_002",
ports: { touch: 9002 }, // 群控模式仅需触控端口
},
{
ip: "192.168.1.102",
deviceId: "device_003",
ports: { touch: 9003 }, // 群控模式仅需触控端口
},
];
client.joinGroupControl(slaveDevices);leaveGroupControl()
从群控组中移除设备。
typescript
client.leaveGroupControl(config: VmosEdgeClientConfig[]): void参数:
config: 要移除的设备配置数组(VmosEdgeClientConfig[])
示例:
typescript
client.leaveGroupControl([
{
ip: "192.168.1.101",
deviceId: "device_002",
ports: { touch: 9002 },
},
]);状态查询方法
getRunningState()
获取当前运行状态。
typescript
client.getRunningState(): boolean返回值: true 表示正在运行,false 表示已停止。
getVideoSize()
获取视频尺寸信息。
typescript
client.getVideoSize(): { width: number; height: number }返回值: 包含视频宽度和高度的对象(类似 VmosEdgeSizeChangedEvent 中的 videoWidth 和 videoHeight)。
示例:
typescript
const size = client.getVideoSize();
console.log(`视频尺寸: ${size.width} x ${size.height}`);getConfig()
获取设备配置信息。
typescript
client.getConfig(): VmosEdgeClientConfig | null返回值: 设备配置对象(VmosEdgeClientConfig),如果未初始化则返回 null。
getScale()
获取当前缩放比例。
typescript
client.getScale(): number返回值: 缩放比例(显示尺寸 / 视频源尺寸)。
