🐛 Bug fixes and optimization

This commit is contained in:
2024-05-12 20:59:33 +08:00
parent 98547708af
commit 6f7ae4467c
8 changed files with 109 additions and 68 deletions

View File

@ -31,6 +31,8 @@ class ChatProvider extends ChangeNotifier {
PagingController<int, Message>? historyPagingController;
WebSocketChannel? _channel;
Future<WebSocketChannel?> connect(AuthProvider auth) async {
if (auth.client == null) await auth.loadClient();
if (!await auth.isAuthorized()) return null;
@ -46,11 +48,12 @@ class ChatProvider extends ChangeNotifier {
queryParameters: {'tk': Uri.encodeComponent(auth.client!.currentToken!)},
);
final channel = WebSocketChannel.connect(uri);
isOpened = true;
channel.stream.listen(
_channel = WebSocketChannel.connect(uri);
await _channel!.ready;
_channel!.stream.listen(
(event) {
final result = NetworkPackage.fromJson(jsonDecode(event));
if (focusChannel == null || historyPagingController == null) return;
@ -90,11 +93,16 @@ class ChatProvider extends ChangeNotifier {
}
notifyListeners();
},
onError: (_, __) => connect(auth),
onDone: () => connect(auth),
onError: (_, __) => Future.delayed(const Duration(seconds: 3), () => connect(auth)),
onDone: () => Future.delayed(const Duration(seconds: 1), () => connect(auth)),
);
return channel;
return _channel!;
}
void disconnect() {
_channel = null;
isOpened = false;
}
Future<void> fetchMessages(int pageKey, BuildContext context) async {

View File

@ -64,6 +64,8 @@ class NotifyProvider extends ChangeNotifier {
notifyListeners();
}
WebSocketChannel? _channel;
Future<WebSocketChannel?> connect(
AuthProvider auth, {
Keypair? Function(String id)? onKexRequest,
@ -83,10 +85,12 @@ class NotifyProvider extends ChangeNotifier {
queryParameters: {'tk': Uri.encodeComponent(auth.client!.currentToken!)},
);
final channel = WebSocketChannel.connect(uri);
await channel.ready;
isOpened = true;
channel.stream.listen(
_channel = WebSocketChannel.connect(uri);
await _channel!.ready;
_channel!.stream.listen(
(event) {
final result = NetworkPackage.fromJson(jsonDecode(event));
switch (result.method) {
@ -101,7 +105,7 @@ class NotifyProvider extends ChangeNotifier {
if (onKexRequest == null || result.payload == null) break;
final resp = onKexRequest(result.payload!['keypair_id']);
if (resp == null) break;
channel.sink.add(jsonEncode(
_channel!.sink.add(jsonEncode(
NetworkPackage(method: 'kex.provide', payload: {
'request_id': result.payload!['request_id'],
'keypair_id': resp.id,
@ -121,11 +125,16 @@ class NotifyProvider extends ChangeNotifier {
break;
}
},
onError: (_, __) => connect(auth),
onDone: () => connect(auth),
onError: (_, __) => Future.delayed(const Duration(seconds: 3), () => connect(auth)),
onDone: () => Future.delayed(const Duration(seconds: 1), () => connect(auth)),
);
return channel;
return _channel!;
}
void disconnect() {
_channel = null;
isOpened = false;
}
void notifyMessage(String title, String body) {