You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.1 KiB

import 'dart:convert';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:huixiang/im/Proto.dart';
import 'package:huixiang/im/database/message.dart';
import 'package:huixiang/im/out/auth.pb.dart';
import 'package:huixiang/im/out/message.pb.dart';
import 'package:huixiang/main.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SocketClient {
Socket _socket;
SharedPreferences shared;
connect() async {
shared = await SharedPreferences.getInstance();
4 months ago
await Socket.connect('192.168.10.200', 49168).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)} ');
4 months ago
hxDatabase.insert(createMessage(userId, utf8.decode(data1.data), msgType: data1.type.value, userId: data1.from));
4 months ago
callbacks.values.forEach((callback) {
callback.call(data1);
});
}, onError: (Object error, StackTrace stackTrace) {
4 months ago
debugPrint("socket-listen-error: $error, stackTrace: ${stackTrace}");
});
authRequest(shared.getString("token"));
}).catchError((error) {
debugPrint("socket-connect-error: $error");
4 months ago
Future.delayed(const Duration(milliseconds: 3000), () {
connect();
});
});
}
4 months ago
Map<String, Function> callbacks = <String, Function>{};
4 months ago
addCallback(String userId, Function callback) {
callbacks.putIfAbsent(userId, callback);
}
4 months ago
removeCallback(String userId) {
callbacks.remove(userId);
}
dispose() {
_socket.close();
}
authRequest(String token) {
if (!checkSocket()) {
return;
}
final authReq = AuthReq()
4 months ago
..uid = userId
..token = token;
final authReqBytes = authReq.writeToBuffer();
final proto = Proto(1, 1, authReqBytes); // 假设 operation 和 seqId 为 1
final protoBytes = proto.toBytes();
_socket.add(protoBytes);
}
4 months ago
Future<Message> sendMessage(String toId, String content) async {
Map message = createMessage(toId, content, userId: userId);
int id = await hxDatabase.insert(message).catchError((error) {
debugPrint("insertMessage: $error");
});
if (!checkSocket()) {
4 months ago
hxDatabase.update({"id": id, "state": 3}).catchError((error) {
debugPrint("insertMessage: $error");
});
message["id"] = id;
message["state"] = 3;
return Message.fromJson(message);
}
4 months ago
message["id"] = id;
Uint8List data = utf8.encode(content);
MsgData msgData = MsgData(to: toId, from: userId, type: MsgType.COMMAND, data: data);
final proto2 = Proto(5, 1, msgData.writeToBuffer());
_socket.add(proto2.toBytes());
4 months ago
debugPrint("sendMessage: ${message["id"]}");
return Message.fromJson(message);
}
checkSocket() {
if (_socket == null) {
connect();
return false;
}
return true;
}
4 months ago
get userId => shared.getString("userId");
}