|
|
|
@ -1,9 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import 'dart:convert'; |
|
|
|
|
import 'dart:core'; |
|
|
|
|
import 'dart:core'; |
|
|
|
|
import 'dart:io'; |
|
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart'; |
|
|
|
|
import 'package:huixiang/im/Proto.dart'; |
|
|
|
|
import 'package:huixiang/im/database/message.dart'; |
|
|
|
@ -20,40 +18,48 @@ class SocketClient {
|
|
|
|
|
connect() async { |
|
|
|
|
shared = await SharedPreferences.getInstance(); |
|
|
|
|
|
|
|
|
|
await Socket.connect('192.168.10.200', 49168).then((value) { |
|
|
|
|
await Socket.connect('192.168.10.129', 9090).then((value) { |
|
|
|
|
debugPrint("socket-connect"); |
|
|
|
|
_socket = value; |
|
|
|
|
_socket.listen((data) { |
|
|
|
|
print(data); |
|
|
|
|
print("socket-listen"); |
|
|
|
|
Proto proto = Proto.fromBytes(data); |
|
|
|
|
MsgData data1 = MsgData.fromBuffer(proto.body); |
|
|
|
|
print('收到来自:${data1.from},消息内容: ${utf8.decode(data1.data)} '); |
|
|
|
|
MsgData dataResult = MsgData.fromBuffer(proto.body); |
|
|
|
|
print('收到来自:${dataResult.from},消息内容: ${utf8.decode(dataResult.data)} '); |
|
|
|
|
|
|
|
|
|
hxDatabase.insert(createMessage(userId, utf8.decode(data1.data), msgType: data1.type.value, userId: data1.from)); |
|
|
|
|
Map<String, dynamic> messageMap = createMessage(userId, utf8.decode(dataResult.data), msgType: dataResult.type.value, userId: dataResult.from); |
|
|
|
|
Message message = Message.fromJson(messageMap); |
|
|
|
|
callbacks[userId]?.call(message); /// user self conversation callback |
|
|
|
|
callbacks[dataResult.from]?.call(message); /// user conversation callback |
|
|
|
|
|
|
|
|
|
callbacks.values.forEach((callback) { |
|
|
|
|
callback.call(data1); |
|
|
|
|
}); |
|
|
|
|
hxDatabase.insert(messageMap); |
|
|
|
|
|
|
|
|
|
}, onError: (Object error, StackTrace stackTrace) { |
|
|
|
|
debugPrint("socket-listen-error: $error, stackTrace: ${stackTrace}"); |
|
|
|
|
debugPrint("socket-listen-error: $error, stackTrace: $stackTrace"); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
authRequest(shared.getString("token")); |
|
|
|
|
|
|
|
|
|
}).catchError((error) { |
|
|
|
|
debugPrint("socket-connect-error: $error"); |
|
|
|
|
Future.delayed(const Duration(milliseconds: 3000), () { |
|
|
|
|
connect(); |
|
|
|
|
reconnect(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int reconnectTime = 1500; |
|
|
|
|
|
|
|
|
|
reconnect() { |
|
|
|
|
Future.delayed(Duration(milliseconds: reconnectTime *= 2), () { |
|
|
|
|
dispose(); |
|
|
|
|
connect(); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Map<String, Function> callbacks = <String, Function>{}; |
|
|
|
|
Map<String, Function(Message message)> callbacks = <String, Function(Message message)>{}; |
|
|
|
|
|
|
|
|
|
addCallback(String userId, Function callback) { |
|
|
|
|
callbacks.putIfAbsent(userId, callback); |
|
|
|
|
addCallback(String userId, callback) { |
|
|
|
|
callbacks[userId] = callback; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
removeCallback(String userId) { |
|
|
|
@ -61,8 +67,10 @@ class SocketClient {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
dispose() { |
|
|
|
|
if (_socket != null) { |
|
|
|
|
_socket.close(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
authRequest(String token) { |
|
|
|
|
if (!checkSocket()) { |
|
|
|
@ -78,7 +86,7 @@ class SocketClient {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Future<Message> sendMessage(String toId, String content) async { |
|
|
|
|
Map message = createMessage(toId, content, userId: userId); |
|
|
|
|
Map<String, dynamic> message = createMessage(toId, content, userId: userId); |
|
|
|
|
int id = await hxDatabase.insert(message).catchError((error) { |
|
|
|
|
debugPrint("insertMessage: $error"); |
|
|
|
|
}); |
|
|
|
@ -92,7 +100,7 @@ class SocketClient {
|
|
|
|
|
} |
|
|
|
|
message["id"] = id; |
|
|
|
|
Uint8List data = utf8.encode(content); |
|
|
|
|
MsgData msgData = MsgData(to: toId, from: userId, type: MsgType.COMMAND, data: data); |
|
|
|
|
MsgData msgData = MsgData(to: toId, from: userId, type: MsgType.TEXT, data: data); |
|
|
|
|
final proto2 = Proto(5, 1, msgData.writeToBuffer()); |
|
|
|
|
_socket.add(proto2.toBytes()); |
|
|
|
|
debugPrint("sendMessage: ${message["id"]}"); |
|
|
|
@ -101,13 +109,12 @@ class SocketClient {
|
|
|
|
|
|
|
|
|
|
checkSocket() { |
|
|
|
|
if (_socket == null) { |
|
|
|
|
connect(); |
|
|
|
|
reconnect(); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get userId => shared.getString("userId"); |
|
|
|
|
|
|
|
|
|
String get userId => shared.getString("userId"); |
|
|
|
|
|
|
|
|
|
} |