loris/lib/business_logic/websocket.dart

43 lines
1.1 KiB
Dart
Raw Normal View History

2022-08-15 13:08:37 +00:00
import 'package:web_socket_channel/io.dart';
import '../global.dart' as global;
2022-08-13 16:14:12 +00:00
2022-08-15 13:08:37 +00:00
IOWebSocketChannel? channel;
2022-08-13 16:14:12 +00:00
bool connected = false;
2022-08-15 13:08:37 +00:00
Map<String, IOWebSocketChannel> map = {};
Future<void> reloadWebsockets() async {
closeSockets();
for (int i = 0; i < global.settings!.identities.length; i++) {
final idName = global.settings!.identities.keys.toList()[i];
final id = global.settings!.identities[idName]!;
2022-08-15 13:53:38 +00:00
var query = {
"stream": "user",
"access_token": id.token,
};
var host = id.webSocketUrl;
host = host.replaceFirstMapped("wss://", (match) => "");
host = host.replaceFirstMapped("ws://", (match) => "");
final uri = Uri(
scheme: "wss",
host: host,
path: "/api/v1/streaming",
queryParameters: query);
2022-08-15 13:08:37 +00:00
map.addAll(
{
idName: IOWebSocketChannel.connect(
uri,
2022-08-15 13:53:38 +00:00
headers: global.defaultHeaders,
2022-08-15 13:08:37 +00:00
),
},
);
}
}
void closeSockets() {
for (int i = 0; i < map.length; i++) {
String key = map.keys.toList()[i];
map[key]!.sink.close();
}
}