How to decode objects sent from netty server and encode objects to netty server in Dart

时间:2019-04-17 00:12:41

标签: dart flutter netty

I have made a question to send/receive from netty server to flutter client already, but I have succeeded in creating the connection. However, my new problem is that the data sent from the netty server is a byte array (I believe anyways) and I can't seem to figure out how to decode the byte array to object. I still would like to have my original question answered since an answer could be a better method than what I am doing at the moment. How would I go from decrypting a byte array sent from a netty server (code here) and encoding a byte array? (I am using a ObjectEncoder and ObjectDecoder for netty)

1 个答案:

答案 0 :(得分:2)

netty has different codecs for serializing data. The default ObjectEncoder appears to serialize a header including the java class uid and then appends a normal java object serialization. It's unlikely that you will want to implement a counterpart to that in Dart.

You need to find a data representation that you can generate/parse in netty and Dart. At one extreme you could use netty ByteBuf. It'll be fairly clear how netty is serializing into ByteBuf and you can mimic that at the Dart end using ByteData. At the other extreme, you might barely use netty's serialization. You could encode your Java object into a utf8 JSON string, send that, and at the dart end simply decode the utf8 and the json. Of course, that doesn't give you a compact wire representation. If you want a compact representation that's available in Java and Dart, consider protobuf.

If you are getting two systems in two different languages to interoperate, you need to be in charge of how they are both doing the serialization/deserialization. Using netty's ObjectEncoder seems like a bad idea as it relies too heavily on Java's internal object serialization, which has no counterpart at the Dart end.