my app is using WebRTC to communicate with a browser using Chrome (PeerJS), actually multiple browsers. So, whenever I create a block within the browser, the peers are supposed to pass to each other the info of that browser, as far as I know it should be a JSON. The thing is, I cannot get to format it the right way, there are always messed up characters in the message I receive, even if the information like previous block are correct. I just feel like this shouldn't be happening.
I'm trying to figure out what is going on, I feel like PeerJS is doing something more other than sending just a pure JSON. When my app communicate through WebSockets for Offer/Answer/Candidate, all their JSON comes in a normal formating.
My code for onMessage for my RTC Datachannel is:
@Override
public void onMessage(DataChannel.Buffer buffer) {
Charset utf8 = Charset.forName("UTF-8");
ByteBuffer byteBuffer = buffer.data;
CharBuffer charBuffer = utf8.decode(byteBuffer);
Log.d(TAG, "onMessage: " + byteBuffer.toString());
Log.d(TAG, "onMessage: " + charBuffer.toString());
}
The messages I get from the browsers:
��typem�data��type�block��header��index�timestamp�����d�o���previousHash���@0ff530e5a7f0f7d88189e1a87c380cbe0a1a5de9a904278c4831592b0bfd7017�hash���@0d15980b550ce37fe347d08d27e7806980aa8fb65663e667c9c6630de7d69e8e�data��type�ART�timestamp�����d�o���contexthash���@a46887f22840ca5e7ac2368e1c090b3feab8f238788be71864831b48cac45a8f���requestingAcessPKey���VbBtHUR-LkiTMYpxrcF9MofNFa_fgHWLTQkpfSEvo1nksRmsUBiiG7k9eNbOjZ4IDPp61IO4BnA7hz4JiahslxM�signedMsg����0645fc574d2a2ea04018baf91f3b030dea3a4b66a862ae7ad5d6bd8c9d35ddbd18f49b853d75fd7578361046e28104bc6565c2aeb7df7aa7ea120851ea4b6fbf
Also, the message is in a binary format.
Answering my own question. The problem was that PeerJS uses a JS-binary serialization library, in order to deserialize it I needed to write that lib into Java code.
There was no way with raw Android/Java to do it.
The JS library is called binarypackJS.
(Also worth mentioning, that you can actually use JSON to communicate with peerJS, all you need to do is to enable msg type as 'json' and not 'binary'.