Unity-Technologies / furioos-sdk-js

Furioos SDK for JavaScript
30 stars 14 forks source link

JSON.Parse exception with debugAppMode: true #45

Open azixMcAze opened 3 years ago

azixMcAze commented 3 years ago

When testing the Furioos SDK with debugAppMode: true, an exception is thrown when the sign-in message ({"type" :"signIn","peerName" :"SDK_APP"}) is received from the application:

Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
    _wsOnMessage SDKDebug.js:33
    onmessage SDKDebug.js:12
    SDKDebug SDKDebug.js:12
    Player Player.js:112
    [1]< index.js:6
    o _prelude.js:1
    r _prelude.js:1
    <anonymous> _prelude.js:1

It seems that JSON.parse is passed an ArrayBuffer instead of a string in _wsOnMessage

This can be fixed by decoding the ArrayBuffer before parsing its content:

  decoder = new TextDecoder()

  _wsOnMessage(event) {
    const str = this.decoder.decode(event.data);
    const msg = JSON.parse(str);
...

However, the message will be discarded because it is not recognized by the following test:

...
  if (msg.type == "furioos" && msg.task == "sdk") {
...
pho3nixf1re commented 2 years ago

I can confirm the same behavior. Are there any workarounds?