🐛 Trying to fix stream already listen

This commit is contained in:
LittleSheep 2025-02-07 21:30:52 +08:00
parent 167c11b9eb
commit a2d2ce4d38
4 changed files with 11 additions and 8 deletions

View File

@ -71,7 +71,7 @@ class ChatMessageController extends ChangeNotifier {
resp.data as Map<String, dynamic>, resp.data as Map<String, dynamic>,
); );
_wsSubscription = _ws.stream.stream.listen((event) { _wsSubscription = _ws.pk.stream.listen((event) {
switch (event.method) { switch (event.method) {
case 'events.new': case 'events.new':
if (event.payload?['channel_id'] != channel?.id) break; if (event.payload?['channel_id'] != channel?.id) break;

View File

@ -77,7 +77,7 @@ class NotificationProvider extends ChangeNotifier {
List<SnNotification> notifications = List.empty(growable: true); List<SnNotification> notifications = List.empty(growable: true);
void listen() { void listen() {
_ws.stream.stream.listen((event) { _ws.pk.stream.listen((event) {
if (event.method == 'notifications.new') { if (event.method == 'notifications.new') {
final notification = SnNotification.fromJson(event.payload!); final notification = SnNotification.fromJson(event.payload!);
if (showingCount < 0) showingCount = 0; if (showingCount < 0) showingCount = 0;

View File

@ -18,7 +18,8 @@ class WebSocketProvider extends ChangeNotifier {
late final SnNetworkProvider _sn; late final SnNetworkProvider _sn;
late final UserProvider _ua; late final UserProvider _ua;
StreamController<WebSocketPackage> stream = StreamController.broadcast(); StreamController<WebSocketPackage> pk = StreamController.broadcast();
Stream<dynamic>? _wsStream;
WebSocketProvider(BuildContext context) { WebSocketProvider(BuildContext context) {
_sn = context.read<SnNetworkProvider>(); _sn = context.read<SnNetworkProvider>();
@ -36,7 +37,7 @@ class WebSocketProvider extends ChangeNotifier {
Completer<void>? _connectCompleter; Completer<void>? _connectCompleter;
Future<void> connect({noRetry = false}) async { Future<void> connect({noRetry = false}) async {
if(_connectCompleter != null) { if (_connectCompleter != null) {
await _connectCompleter!.future; await _connectCompleter!.future;
_connectCompleter = null; _connectCompleter = null;
} }
@ -59,6 +60,7 @@ class WebSocketProvider extends ChangeNotifier {
try { try {
conn = WebSocketChannel.connect(uri); conn = WebSocketChannel.connect(uri);
await conn!.ready; await conn!.ready;
_wsStream = conn!.stream.asBroadcastStream();
listen(); listen();
log('[WebSocket] Connected to server!'); log('[WebSocket] Connected to server!');
isConnected = true; isConnected = true;
@ -73,7 +75,7 @@ class WebSocketProvider extends ChangeNotifier {
log('Retry connecting to websocket in 3 seconds...'); log('Retry connecting to websocket in 3 seconds...');
return Future.delayed( return Future.delayed(
const Duration(seconds: 3), const Duration(seconds: 3),
() => connect(noRetry: true), () => connect(noRetry: true),
); );
} }
} finally { } finally {
@ -93,11 +95,12 @@ class WebSocketProvider extends ChangeNotifier {
} }
void listen() { void listen() {
conn?.stream.listen( if (_wsStream == null) return;
_wsStream!.listen(
(event) { (event) {
final packet = WebSocketPackage.fromJson(jsonDecode(event)); final packet = WebSocketPackage.fromJson(jsonDecode(event));
log('Websocket incoming message: ${packet.method} ${packet.message}'); log('Websocket incoming message: ${packet.method} ${packet.message}');
stream.sink.add(packet); pk.sink.add(packet);
}, },
onDone: () { onDone: () {
isConnected = false; isConnected = false;

View File

@ -206,7 +206,7 @@ class _ChatRoomScreenState extends State<ChatRoomScreen> {
}); });
final ws = context.read<WebSocketProvider>(); final ws = context.read<WebSocketProvider>();
_wsSubscription = ws.stream.stream.listen((event) { _wsSubscription = ws.pk.stream.listen((event) {
switch (event.method) { switch (event.method) {
case 'calls.new': case 'calls.new':
final payload = SnChatCall.fromJson(event.payload!); final payload = SnChatCall.fromJson(event.payload!);