project: 增加web和adb控制,未测试
This commit is contained in:
31
WebRTCControllerWeb/src/proto/controlMessage.js
Normal file
31
WebRTCControllerWeb/src/proto/controlMessage.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import protobuf from 'protobufjs';
|
||||
|
||||
// 与 webrtc_controller_flutter/proto/control_message.proto 完全一致的运行时编码。
|
||||
// 浏览器端通过 protobufjs 动态加载 .proto,构造 ControlMessage 并序列化为二进制,
|
||||
// 经 DataChannel 发送,由 Android 被控端 InputCommandHandler 解析执行。
|
||||
|
||||
let ControlMessage = null;
|
||||
|
||||
export const Action = {
|
||||
ACTION_UNKNOWN: 0,
|
||||
TOUCH: 1,
|
||||
SWIPE: 2,
|
||||
KEY: 3,
|
||||
LONG_PRESS: 4,
|
||||
MOTION_EVENT: 5,
|
||||
};
|
||||
|
||||
export async function loadProto() {
|
||||
if (ControlMessage) return;
|
||||
const root = await protobuf.load(`${import.meta.env.BASE_URL}control_message.proto`);
|
||||
ControlMessage = root.lookupType('com.ttstd.control.ControlMessage');
|
||||
}
|
||||
|
||||
export function encodeControlMessage(fields) {
|
||||
if (!ControlMessage) throw new Error('protobuf 尚未加载,请先调用 loadProto()');
|
||||
const err = ControlMessage.verify(fields);
|
||||
if (err) throw new Error(err);
|
||||
const message = ControlMessage.create(fields);
|
||||
// 返回 Uint8Array,可直接通过 RTCDataChannel.send 发送(二进制)。
|
||||
return ControlMessage.encode(message).finish();
|
||||
}
|
||||
Reference in New Issue
Block a user