fix(websocket): handle notification wrapper, hyphenated types, and stringified data
This commit is contained in:
@@ -14,10 +14,20 @@ const _alertTypes = {
|
|||||||
|
|
||||||
WebSocketEvent? parseWebSocketEvent(String raw) {
|
WebSocketEvent? parseWebSocketEvent(String raw) {
|
||||||
try {
|
try {
|
||||||
final json = jsonDecode(raw) as Map<String, dynamic>;
|
var json = jsonDecode(raw) as Map<String, dynamic>;
|
||||||
final type = json['type'] as String? ?? '';
|
|
||||||
|
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? ?? '';
|
final timestamp = json['timestamp'] as String? ?? '';
|
||||||
|
|
||||||
|
type = type.replaceAll('-', '_');
|
||||||
|
|
||||||
if (_alertTypes.contains(type)) {
|
if (_alertTypes.contains(type)) {
|
||||||
return AlertEvent(
|
return AlertEvent(
|
||||||
type: type,
|
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) {
|
return switch (type) {
|
||||||
'position' => PositionEvent(
|
'position' => PositionEvent(
|
||||||
@@ -88,20 +101,20 @@ WebSocketEvent? parseWebSocketEvent(String raw) {
|
|||||||
[],
|
[],
|
||||||
timestamp: timestamp,
|
timestamp: timestamp,
|
||||||
),
|
),
|
||||||
'video-call' => VideoCallEvent(
|
'video_call' => VideoCallEvent(
|
||||||
chatType: data['chatType'] as String? ?? '',
|
chatType: data['chatType'] as String? ?? '',
|
||||||
appAccount: data['appAccount'] as String? ?? '',
|
appAccount: data['appAccount'] as String? ?? '',
|
||||||
roomNumber: data['roomNumber'] as String? ?? '',
|
roomNumber: data['roomNumber'] as String? ?? '',
|
||||||
sessionId: data['sessionId'] as String?,
|
sessionId: data['sessionId'] as String?,
|
||||||
timestamp: timestamp,
|
timestamp: timestamp,
|
||||||
),
|
),
|
||||||
'video-call-refused' => VideoCallRefusedEvent(
|
'video_call_refused' => VideoCallRefusedEvent(
|
||||||
chatType: data['chatType'] as String? ?? '',
|
chatType: data['chatType'] as String? ?? '',
|
||||||
appAccount: data['appAccount'] as String? ?? '',
|
appAccount: data['appAccount'] as String? ?? '',
|
||||||
roomNumber: data['roomNumber'] as String? ?? '',
|
roomNumber: data['roomNumber'] as String? ?? '',
|
||||||
timestamp: timestamp,
|
timestamp: timestamp,
|
||||||
),
|
),
|
||||||
'video-call-room-count' => VideoCallRoomCountEvent(
|
'video_call_room_count' => VideoCallRoomCountEvent(
|
||||||
chatType: data['chatType'] as String? ?? '',
|
chatType: data['chatType'] as String? ?? '',
|
||||||
count: data['count'] as int? ?? 0,
|
count: data['count'] as int? ?? 0,
|
||||||
roomNumber: data['roomNumber'] as String? ?? '',
|
roomNumber: data['roomNumber'] as String? ?? '',
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ class WebSocketService {
|
|||||||
|
|
||||||
void _onMessage(dynamic raw) {
|
void _onMessage(dynamic raw) {
|
||||||
if (raw is! String) return;
|
if (raw is! String) return;
|
||||||
|
debugPrint('[WebSocket] Message: $raw');
|
||||||
final event = parseWebSocketEvent(raw);
|
final event = parseWebSocketEvent(raw);
|
||||||
if (event != null) _eventController.add(event);
|
if (event != null) _eventController.add(event);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user