2024-04-18 14:33:55 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/utils/service_url.dart';
|
|
|
|
import 'package:web_socket_channel/web_socket_channel.dart';
|
|
|
|
|
|
|
|
class ChatProvider {
|
|
|
|
bool isOpened = false;
|
|
|
|
|
|
|
|
Future<WebSocketChannel?> connect(AuthProvider auth) async {
|
|
|
|
if (auth.client == null) await auth.pickClient();
|
|
|
|
if (!await auth.isAuthorized()) return null;
|
|
|
|
|
|
|
|
await auth.refreshToken();
|
|
|
|
|
2024-04-25 13:33:53 +00:00
|
|
|
var ori = getRequestUri('messaging', '/api/ws');
|
2024-04-18 14:33:55 +00:00
|
|
|
var uri = Uri(
|
|
|
|
scheme: ori.scheme.replaceFirst('http', 'ws'),
|
|
|
|
host: ori.host,
|
|
|
|
path: ori.path,
|
|
|
|
queryParameters: {'tk': Uri.encodeComponent(auth.client!.credentials.accessToken)},
|
|
|
|
);
|
|
|
|
|
|
|
|
final channel = WebSocketChannel.connect(uri);
|
|
|
|
await channel.ready;
|
|
|
|
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
}
|