Skip to content

Commit

Permalink
two improves of live room danmu (#5)
Browse files Browse the repository at this point in the history
* feat: use wss instead of tcp connect

* feat: fallback to next server when live room danmu server connect failed
  • Loading branch information
PuerNya authored Nov 19, 2024
1 parent f3a95a2 commit 1a89e17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
7 changes: 5 additions & 2 deletions lib/pages/live_room/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,15 @@ class LiveRoomController extends GetxController {
if (v['status']) {
LiveDanmakuInfo info = v['data'];
// logger.d("info => $info");
List<String> servers = [];
for (final host in info.data.hostList) {
servers.add('wss://${host.host}:${host.wssPort}/sub');
}
msgStream = LiveMessageStream(
streamToken: info.data.token,
roomId: roomId,
uid: GStorage.userInfo.get('userInfoCache')?.mid ?? 0,
host: info.data.hostList[0].host,
port: info.data.hostList[0].port,
servers: servers,
);
msgStream?.addEventListener((obj) {
if (obj['cmd'] == 'DANMU_MSG') {
Expand Down
30 changes: 20 additions & 10 deletions lib/tcp/live.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';

import 'package:PiliPalaX/services/loggeer.dart';
import 'package:brotli/brotli.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

class PackageHeader {
int totalSize;
Expand Down Expand Up @@ -150,16 +151,16 @@ class HeartbeatPackage extends AbstractPackage<dynamic> {
}

class LiveMessageStream {
String streamToken, host;
int roomId, uid, port;
String streamToken;
int roomId, uid;
List<String> servers;
List<void Function(dynamic obj)> eventListeners = [];
LiveMessageStream(
{required this.streamToken,
required this.roomId,
required this.uid,
required this.host,
required this.port});
late Socket socket;
required this.servers});
late WebSocket socket;
bool heartBeat = true;
PiliLogger logger = getLogger();
final String logTag = "LiveStreamService";
Expand All @@ -185,12 +186,20 @@ class LiveMessageStream {

final marshaledData = authPackage.marshal();
logger.d(marshaledData);

try {
socket = await Socket.connect(host, port);
logger.d('$logTag ===> TCP连接建立');
Future<WebSocket> getSocket() async {
for (final server in servers) {
try {
return await WebSocket.connect(server);
} catch (e) {}
}
throw Exception("all servers connect failed");
}

socket = await getSocket();
// logger.d('$logTag ===> TCP连接建立');
socket.add(authPackage.marshal());
logger.d('$logTag ===> 发送认证包');
// logger.d('$logTag ===> 发送认证包');
await for (var data in socket) {
PackageHeader? header = PackageHeader.fromBytesData(data);
if (header != null) {
Expand Down Expand Up @@ -222,7 +231,8 @@ class LiveMessageStream {
}
socket.close();
} catch (e) {
logger.i('$logTag ===> TCP连接失败: $e');
SmartDialog.showToast("弹幕地址链接失败");
// logger.i('$logTag ===> TCP连接失败: $e');
}
}

Expand Down

0 comments on commit 1a89e17

Please sign in to comment.