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.
60 lines
1.4 KiB
60 lines
1.4 KiB
|
|
|
|
|
|
import 'dart:convert'; |
|
import 'dart:io'; |
|
|
|
import 'package:flutter/foundation.dart'; |
|
import 'package:huixiang/im/Proto.dart'; |
|
import 'package:huixiang/im/out/auth.pb.dart'; |
|
import 'package:huixiang/im/out/message.pb.dart'; |
|
|
|
class SocketClient { |
|
|
|
Socket _socket; |
|
|
|
connect() async { |
|
_socket = await Socket.connect('192.168.10.129', 9090); |
|
_socket.listen((data) { |
|
print(data); |
|
print("socket-listen"); |
|
Proto proto = Proto.fromBytes(data); |
|
print("socket-listen: $proto"); |
|
MsgData data1 = MsgData.fromBuffer(proto.body); |
|
print('收到来自:${data1.from},消息内容: ${utf8.decode(data1.data)} '); |
|
|
|
callbacks.forEach((callback) { |
|
callback.call(data1); |
|
}); |
|
|
|
}); |
|
} |
|
|
|
List<Function> callbacks = []; |
|
|
|
addCallback(Function callback) { |
|
callbacks.add(callback); |
|
} |
|
|
|
removeCallback(Function callback) { |
|
callbacks.remove(callback); |
|
} |
|
|
|
authRequest(String token) { |
|
final authReq = AuthReq() |
|
..uid = 1 |
|
..token = '123456'; |
|
final authReqBytes = authReq.writeToBuffer(); |
|
final proto = Proto(1, 1, authReqBytes); // 假设 operation 和 seqId 为 1 |
|
final protoBytes = proto.toBytes(); |
|
_socket.add(protoBytes); |
|
} |
|
|
|
sendMessage(var to, String content) { |
|
Uint8List data = utf8.encode(content); |
|
MsgData msgData = MsgData(to: to,from: 2,type: MsgType.SINGLE_TEXT,data: data); |
|
final proto2 = Proto(5,1,msgData.writeToBuffer()); |
|
_socket.add(proto2.toBytes()); |
|
} |
|
|
|
} |