🐛 Fix connection issue

This commit is contained in:
LittleSheep 2024-05-12 22:17:32 +08:00
parent b4d1d62e9b
commit 8e0e2dacfe
2 changed files with 20 additions and 7 deletions

View File

@ -33,12 +33,10 @@ class ChatProvider extends ChangeNotifier {
WebSocketChannel? _channel; WebSocketChannel? _channel;
Future<WebSocketChannel?> connect(AuthProvider auth) async { Future<WebSocketChannel?> connect(AuthProvider auth, {noRetry = false}) async {
if (auth.client == null) await auth.loadClient(); if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null; if (!await auth.isAuthorized()) return null;
await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
var ori = getRequestUri('messaging', '/api/ws'); var ori = getRequestUri('messaging', '/api/ws');
var uri = Uri( var uri = Uri(
scheme: ori.scheme.replaceFirst('http', 'ws'), scheme: ori.scheme.replaceFirst('http', 'ws'),
@ -50,8 +48,15 @@ class ChatProvider extends ChangeNotifier {
isOpened = true; isOpened = true;
try {
_channel = WebSocketChannel.connect(uri); _channel = WebSocketChannel.connect(uri);
await _channel!.ready; await _channel!.ready;
} catch (e) {
if (!noRetry) {
await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
connect(auth, noRetry: true);
}
}
_channel!.stream.listen( _channel!.stream.listen(
(event) { (event) {

View File

@ -70,6 +70,7 @@ class NotifyProvider extends ChangeNotifier {
AuthProvider auth, { AuthProvider auth, {
Keypair? Function(String id)? onKexRequest, Keypair? Function(String id)? onKexRequest,
Function(Keypair kp)? onKexProvide, Function(Keypair kp)? onKexProvide,
bool noRetry = false,
}) async { }) async {
if (auth.client == null) await auth.loadClient(); if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null; if (!await auth.isAuthorized()) return null;
@ -87,8 +88,15 @@ class NotifyProvider extends ChangeNotifier {
isOpened = true; isOpened = true;
try {
_channel = WebSocketChannel.connect(uri); _channel = WebSocketChannel.connect(uri);
await _channel!.ready; await _channel!.ready;
} catch (e) {
if (!noRetry) {
await auth.client!.refreshToken(auth.client!.currentRefreshToken!);
connect(auth, noRetry: true);
}
}
_channel!.stream.listen( _channel!.stream.listen(
(event) { (event) {