fix(websocket): handle notification wrapper, hyphenated types, and stringified data

This commit is contained in:
2026-04-17 03:06:09 +02:00
parent 238c15888b
commit e148b9fdfa
2 changed files with 20 additions and 6 deletions

View File

@@ -14,10 +14,20 @@ const _alertTypes = {
WebSocketEvent? parseWebSocketEvent(String raw) {
try {
final json = jsonDecode(raw) as Map<String, dynamic>;
final type = json['type'] as String? ?? '';
var json = jsonDecode(raw) as Map<String, dynamic>;
if (json['type'] == 'notification') {
final inner = json['data'];
if (inner is Map<String, dynamic> && inner.containsKey('type')) {
json = inner;
}
}
var type = json['type'] as String? ?? '';
final timestamp = json['timestamp'] as String? ?? '';
type = type.replaceAll('-', '_');
if (_alertTypes.contains(type)) {
return AlertEvent(
type: type,
@@ -26,7 +36,10 @@ WebSocketEvent? parseWebSocketEvent(String raw) {
);
}
final data = json['data'] as Map<String, dynamic>? ?? {};
final rawData = json['data'];
final data = rawData is String
? (jsonDecode(rawData) as Map<String, dynamic>? ?? {})
: (rawData as Map<String, dynamic>? ?? {});
return switch (type) {
'position' => PositionEvent(
@@ -88,20 +101,20 @@ WebSocketEvent? parseWebSocketEvent(String raw) {
[],
timestamp: timestamp,
),
'video-call' => VideoCallEvent(
'video_call' => VideoCallEvent(
chatType: data['chatType'] as String? ?? '',
appAccount: data['appAccount'] as String? ?? '',
roomNumber: data['roomNumber'] as String? ?? '',
sessionId: data['sessionId'] as String?,
timestamp: timestamp,
),
'video-call-refused' => VideoCallRefusedEvent(
'video_call_refused' => VideoCallRefusedEvent(
chatType: data['chatType'] as String? ?? '',
appAccount: data['appAccount'] as String? ?? '',
roomNumber: data['roomNumber'] as String? ?? '',
timestamp: timestamp,
),
'video-call-room-count' => VideoCallRoomCountEvent(
'video_call_room_count' => VideoCallRoomCountEvent(
chatType: data['chatType'] as String? ?? '',
count: data['count'] as int? ?? 0,
roomNumber: data['roomNumber'] as String? ?? '',

View File

@@ -73,6 +73,7 @@ class WebSocketService {
void _onMessage(dynamic raw) {
if (raw is! String) return;
debugPrint('[WebSocket] Message: $raw');
final event = parseWebSocketEvent(raw);
if (event != null) _eventController.add(event);
}