loris/lib/business_logic/websocket.dart

62 lines
1.7 KiB
Dart
Raw Normal View History

2022-08-15 18:31:44 +00:00
import 'dart:async';
import 'package:loris/business_logic/settings.dart';
2022-09-04 08:12:52 +00:00
import 'package:web_socket_channel/web_socket_channel.dart';
2022-08-15 13:08:37 +00:00
import '../global.dart' as global;
2022-08-13 16:14:12 +00:00
bool connected = false;
2022-08-15 13:08:37 +00:00
2022-08-15 18:31:44 +00:00
Map<String, Map<String, StreamController>> map = {};
2022-08-15 13:08:37 +00:00
Future<void> reloadWebsockets() async {
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 18:31:44 +00:00
Map<String, StreamController> idMap = {
"home": getStreamController(id, StreamType.user),
"local": getStreamController(id, StreamType.publicLocal),
"public": getStreamController(id, StreamType.publicLocal),
2022-08-15 13:53:38 +00:00
};
2022-08-15 18:31:44 +00:00
2022-08-15 13:08:37 +00:00
map.addAll(
2022-08-15 18:31:44 +00:00
{idName: idMap},
2022-08-15 13:08:37 +00:00
);
}
}
2022-08-15 18:31:44 +00:00
enum StreamType {
user,
public,
publicLocal,
}
extension StreamTypeExtension on StreamType {
String get name {
switch (this) {
case StreamType.user:
return "user";
case StreamType.public:
return "public";
case StreamType.publicLocal:
return "public:local";
}
2022-08-15 13:08:37 +00:00
}
}
2022-08-15 18:31:44 +00:00
StreamController getStreamController(AccountSettings id, StreamType type) {
var query = {
"stream": type.name,
"access_token": id.token,
};
var host = id.webSocketUrl;
host = host.replaceFirstMapped("wss://", (match) => "");
host = host.replaceFirstMapped("ws://", (match) => "");
const scheme = "wss";
const path = "/api/v1/streaming";
final uri =
Uri(scheme: scheme, host: host, path: path, queryParameters: query);
final controller = StreamController.broadcast();
2022-09-04 08:12:52 +00:00
final socket = WebSocketChannel.connect(uri);
2022-08-15 18:31:44 +00:00
controller.addStream(socket.stream);
return controller;
}