I. Application background:
Protobuf is Google’s format for data exchange, which is language independent and platform independent.
Advantages:
- The advantage of JSON is that it is smaller than XML format, the transmission efficiency is much higher than XML, and the readability is good.
- XML has the advantage of being readable and easy to parse.
- Protobuf has the advantages of fast transfer (said to be 10-20 times faster than XML and JSON for large amounts of data), small serialized size compared to JSON and XML, cross-platform multi-language support, message format upgrade and compatibility is good, and fast serialization and deserialization.
Disadvantages:
- The downside of JSON is that it’s not particularly efficient (faster than XML, but much slower than Protobuf).
- The disadvantage of XML is that it is inefficient and consumes too much resources.
- The downside of the Protobuf is that it’s not very easy to use.
In a scenario that requires a large amount of data to be transmitted, if there is a large amount of data, protobuf can significantly reduce the amount of data to be transmitted, reducing the network I/O, and thus reducing the time spent on the network transmission. Protobuf was a good choice given the amount of message data that would be required for a social focused product, and the need to save traffic.
Second, the use of
1. Import the Protobuf library
pubspec.yaml
. protobuf:1.01.
Copy the code
2. Write proto files
socket.message.proto
syntax = "proto3";
package socket;
// Send a chat message
message Message {
string eventId = 1;
string from = 2;
string to = 3;
string createAt = 4;
string type = 5;
string body = 6; m }// Receive the chat message
message AckMessage {
string eventId = 1;
}
Copy the code
3. Generate proto related Model
Terminal
protoc --dart_out=. socket.message.proto
Copy the code
4. Code and send messages
A. Prepare the Protobuf object
Message message = Message();
message.eventId = '# # # #';
message.type = 'text';
message.body = 'hello world';
Copy the code
P. rotobufUtil coding
const MESSAGE_HEADER_LEN = 2;
/// Data encoding
static List<int> encode(int type, var content) {
ByteData data = ByteData(MESSAGE_HEADER_LEN);
data.setUint16(0, type, Endian.little);
List<int> msg = data.buffer.asUint8List() + content.writeToBuffer().buffer.asUint8List();
return msg;
}
Copy the code
C. to send messages
/// send
sendSocket(int type, var content) async {
IOWebSocketChannel channel = await SocketService.getInstance().getChannel();
if (channel == null) return;
List<int> msg = ProtobufUtil.encode(type, content);
channel.sink.add(msg);
}
sendSocket(11, message)
Copy the code
5. Receive and decode messages
A. the decoding
/// The data decoding
static DecodedMsg decode(data) {
Int8List int8Data = Int8List.fromList(data);
Int8List contentTypeInt8Data = int8Data.sublist(0, MESSAGE_HEADER_LEN);
Int8List contentInt8Data = int8Data.sublist(MESSAGE_HEADER_LEN, int8Data.length);
int contentType = contentTypeInt8Data.elementAt(0);
GeneratedMessage content;
switch (contentType) {
case 10:
content = AckMessage.fromBuffer(contentInt8Data);
break;
case 11:
content = Message.fromBuffer(contentInt8Data);
break;
}
DecodedMsg decodedMsg;
if(contentType ! =null&& content ! =null) {
decodedMsg = DecodedMsg(
contentType: contentType,
content: content,
);
}
return decodedMsg;
}
Copy the code
B. to receive message
channel.stream.listen((data) {
DecodedMsg msg = ProtobufUtil.decode(data);
}
Copy the code
End, scatter flowers 🎉