Browse Source

Merge remote-tracking branch 'origin/wr_202303' into wr_202303

wr_202303
wurong 4 months ago
parent
commit
760c1f6f01
  1. 19
      lib/im/SocketClient.dart

19
lib/im/SocketClient.dart

@ -20,7 +20,7 @@ class SocketClient {
connect() async { connect() async {
shared = await SharedPreferences.getInstance(); shared = await SharedPreferences.getInstance();
await Socket.connect('192.168.10.129', 9090).then((value) { await Socket.connect('192.168.10.200', 49168).then((value) {
debugPrint("socket-connect"); debugPrint("socket-connect");
_socket = value; _socket = value;
_socket.listen((data) { _socket.listen((data) {
@ -32,29 +32,32 @@ class SocketClient {
hxDatabase.insert(createMessage(userId, utf8.decode(data1.data), msgType: data1.type.value, userId: data1.from)); hxDatabase.insert(createMessage(userId, utf8.decode(data1.data), msgType: data1.type.value, userId: data1.from));
callbacks.forEach((callback) { callbacks.values.forEach((callback) {
callback.call(data1); callback.call(data1);
}); });
}, onError: (Object error, StackTrace stackTrace) { }, onError: (Object error, StackTrace stackTrace) {
debugPrint("socket-error: $error, stackTrace: ${stackTrace}"); debugPrint("socket-listen-error: $error, stackTrace: ${stackTrace}");
}); });
authRequest(shared.getString("token")); authRequest(shared.getString("token"));
}).catchError((error) { }).catchError((error) {
debugPrint("socket-connect-error: $error"); debugPrint("socket-connect-error: $error");
Future.delayed(const Duration(milliseconds: 3000), () {
connect();
});
}); });
} }
List<Function> callbacks = []; Map<String, Function> callbacks = <String, Function>{};
addCallback(Function callback) { addCallback(String userId, Function callback) {
callbacks.add(callback); callbacks.putIfAbsent(userId, callback);
} }
removeCallback(Function callback) { removeCallback(String userId) {
callbacks.remove(callback); callbacks.remove(userId);
} }
dispose() { dispose() {

Loading…
Cancel
Save