Dio 使用 proto
- 在pubspec.yaml 安装 dio
dio: ^4.0.6
-
编译protobuf文件 proto转为dart -
发送和接收proto, 可以根据个人的需求,进行封装
static ProPost (Function callBack) async {
var proto = SearchUserInfoRequest.create();
var userinfo = HttpUserInfo.create();
var url = "";
userinfo.userGuid = "22222";
userinfo.userName = "jack";
Uint8List buf = userinfo.writeToBuffer();
Options option = Options(
headers: {
Headers.contentTypeHeader: "application/x-www-form-urlencoded",
Headers.accepHeader: "*"
},
method:"POST",
contentType:"application/x-www-form-urlencoded",
responseType: ResponseType.bytes,
);
var dio = Dio();
try {
final response = await dio.request(url,options:option,data:buf);
DevicesAndGroupsMessage tlcs = DevicesAndGroupsMessage.fromBuffer(response.data);
} on DioError catch (e) {
var ei = e.response?.data;
print(ei.runtimeType);
TlcsErrorRequest tlcs = TlcsErrorRequest.fromBuffer(ei);
return tlcs;
}
return;
callBack(tlcs);
}
- 修改dio插件
进入dio.dart 文件中找到
late Transformer transformer;
然后进入transformer.dart 文件,修改transformRequest 这个方法, 我们会发现在transformRequest中 数据必须是 String ,我们需要数据为Uint8List ,dio 仅仅只是data.toString() ,显然这样是不支持发送Uint8List数据的,因此我们修改一下
修改前:
Future<String> transformRequest(RequestOptions options);
@override
Future<String> transformRequest(RequestOptions options) async {
var data = options.data ?? '';
if (data is! String) {
if (Transformer.isJsonMimeType(options.contentType)) {
return json.encode(options.data);
} else if (data is Map) {
options.contentType =
options.contentType ?? Headers.formUrlEncodedContentType;
return Transformer.urlEncodeMap(data);
}
}
return data.toString();
}
修改后
Future<String> transformRequest(RequestOptions options);
@override
Future<String> transformRequest(RequestOptions options) async {
var data = options.data ?? '';
if (data is! String) {
if (Transformer.isJsonMimeType(options.contentType)) {
return json.encode(options.data);
} else if (data is Map) {
options.contentType =
options.contentType ?? Headers.formUrlEncodedContentType;
return Transformer.urlEncodeMap(data);
} else if(data is Uint8List) {
return String.fromCharCodes(data);
}
}
return data.toString();
}
Socket 使用 proto
- 在pubspec.yaml 安装 web_socket_channel
web_socket_channel: ^1.1.0
-
编译protobuf文件 proto转为dart -
发送和接收proto, 可以根据个人的需求,进行封装
class ProtoMsg {
static late Socket channel;
static createWs(Ip,Prot) async {
await Socket.connect(Ip,Prot,timeout: const Duration(seconds:10)).then((value) {
channel = value;
channel.asBroadcastStream();
}).catchError((onError) {
createWs(Ip,Prot);
});
}
static void dispos() {
channel.close();
}
static writeData(buffer) async {
channel.add(buffer);
}
static onReceiver(event) {
print(event);
ProtoMsg.parseData(event);
}
static parseData(Uint8List event) async {
TlcsErrorRequest tlcs = TlcsErrorRequest.fromBuffer(event);
}
}
ProtoMsg.createWs("","");
var userinfo = HttpUserInfo.create();
userinfo.userGuid = "22222";
userinfo.userName = "jack";
Uint8List buf = userinfo.writeToBuffer();
ProtoMsg.channel.listen(onReceiver);
ProtoMsg.writeData(buf);
|