- 新增 PinStore:首次连接记录被控端签名指纹,变化时弹窗确认 - 新增 SignalingWatch:订阅 /watch,300ms 去抖后重拉设备列表 - 设置页新增"连接走信令中继",主控端按该开关走 ttstd_relay_start - 中继模式禁用投屏并提示原因,状态卡区分直连/中继 - 新增 ControlFileClient:通过 msgType=7 分片上传/列目录,文件页加入口 - NativeCore/Bridge 暴露 relayStart/relayStop/sendFileRequest
142 lines
4.1 KiB
Dart
142 lines
4.1 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
/// 信令服务器 `WS /watch` 的客户端。
|
|
///
|
|
/// 服务器在连接建立后先推一条 `{"event":"snapshot","devices":[...]}`,之后推
|
|
/// `online` / `offline` / `heartbeat`。这里只把事件翻译成「设备列表可能变了」,
|
|
/// 具体拉取交给调用方:`online` 事件里没有完整设备信息(端口/指纹/分辨率),
|
|
/// 与其在这里补一套字段解析,不如让调用方复用已有的 `/list` + 信标发现逻辑。
|
|
class SignalingWatch {
|
|
SignalingWatch({
|
|
required this.signalingUrl,
|
|
required this.onChanged,
|
|
this.onStateChanged,
|
|
});
|
|
|
|
/// 信令服务基地址(http/https),内部会换成 ws/wss。
|
|
final String signalingUrl;
|
|
|
|
/// 设备列表可能发生变化(snapshot / online / offline)。
|
|
final void Function() onChanged;
|
|
|
|
/// 连接状态变化,用于界面提示与诊断日志。
|
|
final void Function(bool connected)? onStateChanged;
|
|
|
|
static const Duration _maxBackoff = Duration(seconds: 15);
|
|
static const Duration _connectTimeout = Duration(seconds: 5);
|
|
|
|
WebSocket? _socket;
|
|
StreamSubscription<dynamic>? _subscription;
|
|
Timer? _reconnectTimer;
|
|
Duration _backoff = const Duration(seconds: 1);
|
|
bool _stopped = false;
|
|
bool _connected = false;
|
|
|
|
bool get isConnected => _connected;
|
|
|
|
/// 把信令基地址换成 `/watch` 的 WebSocket 地址。
|
|
///
|
|
/// 支持带前缀的部署(`https://host/api/` → `wss://host/api/watch`)。
|
|
static Uri? watchUri(String base) {
|
|
final trimmed = base.trim();
|
|
if (trimmed.isEmpty) return null;
|
|
final uri = Uri.tryParse(trimmed);
|
|
if (uri == null || uri.host.isEmpty) return null;
|
|
final scheme = switch (uri.scheme) {
|
|
'https' || 'wss' => 'wss',
|
|
'http' || 'ws' => 'ws',
|
|
_ => null,
|
|
};
|
|
if (scheme == null) return null;
|
|
final path = uri.path.endsWith('/') ? '${uri.path}watch' : '${uri.path}/watch';
|
|
return uri.replace(scheme: scheme, path: path);
|
|
}
|
|
|
|
void start() {
|
|
_stopped = false;
|
|
_backoff = const Duration(seconds: 1);
|
|
unawaited(_connect());
|
|
}
|
|
|
|
Future<void> _connect() async {
|
|
if (_stopped) return;
|
|
final uri = watchUri(signalingUrl);
|
|
if (uri == null) {
|
|
_scheduleReconnect();
|
|
return;
|
|
}
|
|
try {
|
|
final socket = await WebSocket.connect(uri.toString()).timeout(_connectTimeout);
|
|
if (_stopped) {
|
|
await socket.close();
|
|
return;
|
|
}
|
|
_socket = socket;
|
|
_backoff = const Duration(seconds: 1);
|
|
_setConnected(true);
|
|
_subscription = socket.listen(
|
|
_handleMessage,
|
|
onDone: _handleClosed,
|
|
onError: (Object _) => _handleClosed(),
|
|
cancelOnError: true,
|
|
);
|
|
} on Object {
|
|
_scheduleReconnect();
|
|
}
|
|
}
|
|
|
|
void _handleMessage(dynamic data) {
|
|
if (data is! String) return;
|
|
Object? decoded;
|
|
try {
|
|
decoded = jsonDecode(data);
|
|
} on FormatException {
|
|
return;
|
|
}
|
|
if (decoded is! Map) return;
|
|
final event = decoded['event']?.toString();
|
|
if (event == 'snapshot' || event == 'online' || event == 'offline') {
|
|
onChanged();
|
|
}
|
|
// heartbeat 不触发拉取:设备信息没变,只会白刷一遍 /list。
|
|
}
|
|
|
|
void _handleClosed() {
|
|
_subscription?.cancel();
|
|
_subscription = null;
|
|
_socket = null;
|
|
_setConnected(false);
|
|
_scheduleReconnect();
|
|
}
|
|
|
|
void _setConnected(bool value) {
|
|
if (_connected == value) return;
|
|
_connected = value;
|
|
onStateChanged?.call(value);
|
|
}
|
|
|
|
void _scheduleReconnect() {
|
|
if (_stopped) return;
|
|
_reconnectTimer?.cancel();
|
|
// 指数退避,避免信令服务器没启动时每秒重连一次。
|
|
_reconnectTimer = Timer(_backoff, () {
|
|
_backoff = _backoff * 2 > _maxBackoff ? _maxBackoff : _backoff * 2;
|
|
unawaited(_connect());
|
|
});
|
|
}
|
|
|
|
void stop() {
|
|
_stopped = true;
|
|
_reconnectTimer?.cancel();
|
|
_reconnectTimer = null;
|
|
_subscription?.cancel();
|
|
_subscription = null;
|
|
final socket = _socket;
|
|
_socket = null;
|
|
_connected = false;
|
|
if (socket != null) unawaited(socket.close());
|
|
}
|
|
}
|