ClusterWS icon indicating copy to clipboard operation
ClusterWS copied to clipboard

How to do encode objects in to compact byte buffers and then decode them back in to objects on the receiver

Open thaiwu0107 opened this issue 7 years ago • 8 comments

Submitting

  • [ ] Bug
  • [x] Question
  • [ ] Suggestion
  • [ ] Other

Details

How to do encode objects in to compact byte buffers and then decode them back in to objects on the receiver

here is the libs i want to use https://github.com/phretaddin/schemapack

add this in my project

i know this libs have way in pre-process message on receive wss.setMiddleware('onMessageReceive', (socket, message, next) => {}); useBinary option in libs already do binary but not good enough for me

by the way I like this libs

thaiwu0107 avatar Oct 25 '18 02:10 thaiwu0107

There is encodeDecodeEngine options in ClusterWS which has two functions check guide at the bottom, unfortunately this option does not work in Java library yet (should be fixed by next release), all other clients (Swift, JavaScript) support it properly. There is not a lot of documentations for that options as it does not have full support in ClusterWS, but it is literally two functions which get message and return encoded/decoded message back.

goriunov avatar Oct 25 '18 02:10 goriunov

Also check wiki in Javascript client library and Swift

goriunov avatar Oct 25 '18 02:10 goriunov

hi goriunov , thx for your answer but you can see this libs https://github.com/phretaddin/schemapack

in different message need to different schem so that encodeDecodeEngine cant support it

thaiwu0107 avatar Oct 25 '18 03:10 thaiwu0107

You can pass type when u use send method for example:

socket.send("hello world", {type: "player", data: {/* You object **/}})

and then in your encode function

encode: (message) => {
    if(message.type === "player") {
         return  playerSchema.encode(message.data);
    }

    if(message.type === "...") {
      return ...
    }
}

In in your decode function in client you will need to listen to the message on the emit event and decode in each separate emit event (i have not tried that).

goriunov avatar Oct 25 '18 06:10 goriunov

Thx for answer but it will be buffer encode twice or three times or to be string when options use useBinary or encodeDecodeEngine i need it efficient exchange of information。

For example the event or publish comes from or send to other side can use first bit to decide what is this than resolve binary by schem

thaiwu0107 avatar Oct 26 '18 17:10 thaiwu0107

Ohh, unfortunately there is no way to do that yet.

goriunov avatar Oct 26 '18 20:10 goriunov

But event if you decode just your message it will still reduce size quite a bit compering to the default JSON size.

goriunov avatar Oct 26 '18 20:10 goriunov

I will think about that. Probably add some functions to easier modify internal protocol

goriunov avatar Oct 29 '18 01:10 goriunov