import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; import 'package:web_socket_channel/status.dart' as status; import 'package:together_mobile/models/route_state_model.dart'; import 'package:together_mobile/database/box_type.dart'; import 'package:together_mobile/models/apply_list_model.dart'; import 'package:together_mobile/models/contact_model.dart'; import 'package:together_mobile/models/init_get_it.dart'; import 'package:together_mobile/models/user_model.dart'; import 'package:together_mobile/notification_api.dart'; enum SocketStatus { connected, closed, error, } class WebSocketManager extends ChangeNotifier { late Uri wsUrl; late WebSocketChannel channel; String id = ''; SocketStatus socketStatus = SocketStatus.closed; Timer? heartBeatTimer; Timer? serverTimer; Timer? reconnectTimer; int reconnectCount = 30; int reconnectTimes = 0; Duration timeout = const Duration(seconds: 4); void connect(String userId) { id = userId; wsUrl = Uri.parse('ws://10.0.2.2:8000/ws/$id'); channel = WebSocketChannel.connect(wsUrl); socketStatus = SocketStatus.connected; print('websocket connected <$channel>'); heartBeatInspect(); if (reconnectTimer != null) { // reconnectTimer!.cancel(); reconnectTimer = null; reconnectTimes = 0; } channel.stream.listen(onData, onError: onError, onDone: onDone); } void disconnect() { channel.sink.close(); wsUrl = Uri(); id = ''; socketStatus = SocketStatus.closed; heartBeatTimer?.cancel(); serverTimer?.cancel(); reconnectTimer?.cancel(); heartBeatTimer = null; serverTimer = null; reconnectTimer = null; reconnectTimes = 0; } void onData(jsonData) { heartBeatInspect(); Map data = json.decode(jsonData); switch (data['event']) { case 'friend-chat-msg': receiveFriendMsg(data); case 'apply-friend': receiveApplyFriend(data); case 'friend-added': receiveFriendAdded(data); case 'friend-deleted': receiveFriendDeleted(data); case 'friend-chat-image' || 'group-chat-image': receiveChatImages(data); case 'group-chat-creation': receiveGroupChatCreation(data); case 'group-chat-msg': receiveGroupChatMsg(data); } } void onDone() { print('websocket disconnected <$channel>'); if (socketStatus == SocketStatus.closed) { return; } reconnect(); } void onError(Object error) {} void heartBeatInspect() { if (heartBeatTimer != null) { heartBeatTimer!.cancel(); heartBeatTimer = null; } if (serverTimer != null) { serverTimer!.cancel(); serverTimer = null; } print('start heartbeat inspect......'); heartBeatTimer = Timer(timeout, () { channel.sink.add(json.encode({'event': 'ping'})); serverTimer = Timer(timeout, () { channel.sink.close(status.internalServerError); socketStatus = SocketStatus.closed; }); }); } void reconnect() { if (socketStatus == SocketStatus.error) { if (heartBeatTimer != null) { heartBeatTimer!.cancel(); heartBeatTimer = null; } if (serverTimer != null) { serverTimer!.cancel(); serverTimer = null; } return; } print('websocket reconnecting......'); if (heartBeatTimer != null) { heartBeatTimer!.cancel(); heartBeatTimer = null; } if (serverTimer != null) { serverTimer!.cancel(); serverTimer = null; } reconnectTimer = Timer.periodic(timeout, (timer) { if (reconnectTimes < reconnectCount) { connect(id); reconnectTimes++; } else { print('reconnection times exceed the max times......'); // If it is still disconnection after reconnect 30 times, set the socket // status to error, means the network is bad, and stop reconnecting. socketStatus = SocketStatus.error; channel.sink.close(); timer.cancel(); } }); } } void receiveFriendMsg(Map msg) async { print('=================收到了好友信息事件=================='); print(msg); print('======================================='); String senderId = msg['senderId'] as String; late Box messageTBox; try { messageTBox = Hive.box('message_$senderId'); } catch (e) { messageTBox = await Hive.openBox('message_$senderId'); } Box chatSettingBox = Hive.box('chat_setting'); ChatSetting? chatSetting = chatSettingBox.get(senderId); DateTime dateTime = DateTime.parse(msg['dateTime'] as String); if (chatSetting == null) { chatSettingBox.put( senderId, ChatSetting( senderId, 0, false, true, false, dateTime, 1, ), ); } else { chatSetting.isOpen = true; chatSetting.latestDateTime = dateTime; chatSetting.unreadCount++; chatSettingBox.put(senderId, chatSetting); } List attachments = List.from(msg['attachments']); messageTBox.add( MessageT( senderId, msg['text'], msg['type'], DateTime.parse(msg['dateTime'] as String), msg['isShowTime'], attachments, ), ); String name = getIt.get().friends[senderId]!.friendRemark.isEmpty ? getIt.get().friends[senderId]!.nickname : getIt.get().friends[senderId]!.friendRemark; String routeName = getIt.get().currentPathName; String avatar = getIt.get().friends[senderId]!.avatar; if (!getIt.get().isVisible) { NotificationAPI.showMessageNotifications( senderId: senderId, name: name, avatar: avatar, text: msg['text'] as String, ); } else if (routeName == 'Message') { if (!getIt.get().query.containsValue(senderId)) { NotificationAPI.showMessageNotifications( senderId: senderId, name: name, avatar: avatar, text: msg['text'] as String, ); } } else if (routeName != 'Chat') { NotificationAPI.showMessageNotifications( senderId: senderId, name: name, avatar: avatar, text: msg['text'] as String, ); } } void receiveApplyFriend(Map msg) { print('=================收到了申请好友事件=================='); print(msg); print('======================================='); getIt.get().addJson(msg); } void receiveFriendAdded(Map msg) { print('=================收到了申请好友通过事件=================='); print(msg); print('======================================='); getIt.get().addFriend(msg['friendId'], msg['setting']); getIt .get() .addFriendAccountProfile(msg['friendId'], msg['accountProfile']); } void receiveFriendDeleted(Map msg) { print('=================收到了解除好友事件=================='); print(msg); print('======================================='); getIt.get().removeFriend(msg['friendId']); getIt.get().removeFriend(msg['friendId']); } void receiveChatImages(Map msg) async { print('=================收到了聊天图片事件=================='); print(msg); print('======================================='); String chatImageDir = getIt.get().baseImageDir; File file = await File('$chatImageDir/${msg['filename']}').create( recursive: true, ); await file.writeAsBytes(msg['bytes']); } void receiveGroupChatCreation(Map msg) { print('=================收到了群聊邀请事件=================='); print(msg); print('======================================='); String groupChatId = msg['groupChat']['id']; getIt.get().addGroupChat(groupChatId); getIt.get().addGroupChatProfile(groupChatId, msg); } void receiveGroupChatMsg(Map msg) async { print('=================收到了群聊信息事件=================='); print(msg); print('======================================='); String senderId = msg['senderId'] as String; String groupChatId = msg['groupChatId'] as String; late Box messageTBox; try { messageTBox = Hive.box('message_$groupChatId'); } catch (e) { messageTBox = await Hive.openBox('message_$groupChatId'); } Box chatSettingBox = Hive.box('chat_setting'); ChatSetting? chatSetting = chatSettingBox.get(groupChatId); DateTime dateTime = DateTime.parse(msg['dateTime'] as String); if (chatSetting == null) { chatSettingBox.put( groupChatId, ChatSetting( groupChatId, 1, false, true, false, dateTime, 1, ), ); } else { chatSetting.isOpen = true; chatSetting.latestDateTime = dateTime; chatSetting.unreadCount++; chatSettingBox.put(groupChatId, chatSetting); } List attachments = List.from(msg['attachments']); messageTBox.add( MessageT( senderId, msg['text'], msg['type'], DateTime.parse(msg['dateTime'] as String), msg['isShowTime'], attachments, ), ); String avatar = getIt.get().groupChats[groupChatId]!.avatar; late String name; if (getIt.get().friends.containsKey(senderId)) { name = getIt.get().friends[senderId]!.friendRemark.isEmpty ? getIt.get().friends[senderId]!.nickname : getIt.get().friends[senderId]!.friendRemark; } else if (getIt .get() .grouChatMemberProfiles .containsKey(groupChatId)) { if (getIt .get() .grouChatMemberProfiles[groupChatId]! .containsKey(senderId)) { name = getIt .get() .grouChatMemberProfiles[groupChatId]![senderId]! .remark .isEmpty ? getIt .get() .grouChatMemberProfiles[groupChatId]![senderId]! .nickname .isEmpty ? senderId.substring(0, 6) : getIt .get() .grouChatMemberProfiles[groupChatId]![senderId]! .nickname : getIt .get() .grouChatMemberProfiles[groupChatId]![senderId]! .remark; } } else { name = senderId.substring(0, 6); } String routeName = getIt.get().currentPathName; if (!getIt.get().isVisible) { NotificationAPI.showMessageNotifications( senderId: senderId, name: name, avatar: avatar, text: msg['text'] as String, groupChatId: groupChatId, ); } else if (routeName == 'Message') { if (!getIt.get().query.containsValue(senderId)) { NotificationAPI.showMessageNotifications( senderId: senderId, name: name, avatar: avatar, text: msg['text'] as String, groupChatId: groupChatId, ); } } else if (routeName != 'Chat') { NotificationAPI.showMessageNotifications( senderId: senderId, name: name, avatar: avatar, text: msg['text'] as String, groupChatId: groupChatId, ); } }