Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud bug #5

Open
qucchia opened this issue Jun 28, 2021 · 0 comments · May be fixed by #6
Open

Cloud bug #5

qucchia opened this issue Jun 28, 2021 · 0 comments · May be fixed by #6

Comments

@qucchia
Copy link

qucchia commented Jun 28, 2021

Sometimes when trying to connect to the cloud like this:

Client.login().then(() => {
  let cloud = Client.session.createCloudSession(548738926);
  cloud.connect().then(() => {
    console.log("Successfully connected to cloud!");
    cloud.on("set", variable => {
      console.log("Cloud variable", variable.name, "set to", variable.value);
    });
  });
});

The following error appears:

undefined:2
{"method":"set","project_id":"548862682","name":"☁ CloudService response","value":"2410100100050202100307010909100703003819142363351915632419173523"}
^

SyntaxError: Unexpected token { in JSON at position 86
    at JSON.parse (<anonymous>)
    at WebSocket.<anonymous> (/home/runner/dictionary/node_modules/node-scratch-client/src/Struct/CloudSession.js:87:25)
    at WebSocket.emit (events.js:314:20)
    at WebSocket.EventEmitter.emit (domain.js:483:12)
    at Receiver.receiverOnMessage (/home/runner/dictionary/node_modules/node-scratch-client/node_modules/ws/lib/websocket.js:789:20)
    at Receiver.emit (events.js:314:20)
    at Receiver.EventEmitter.emit (domain.js:483:12)
    at Receiver.dataMessage (/home/runner/dictionary/node_modules/node-scratch-client/node_modules/ws/lib/receiver.js:422:14)
    at /home/runner/dictionary/node_modules/node-scratch-client/node_modules/ws/lib/receiver.js:379:23
    at /home/runner/dictionary/node_modules/node-scratch-client/node_modules/ws/lib/permessage-deflate.js:298:9

This is because sometimes the API sends multiple objects at once in a single string, like so:

`{"method":"set","project_id":"548862682","name":"☁ CloudService request","value":"0"}
{"method":"set","project_id":"548862682","name":"☁ CloudService response","value":"2410100100050202100307010909100703003819142363351915632419173523"}`

The way to solve this is simple. Separate the string by line to make an array of chunks, and run the code for every one of them, replacing the function on line 85 in src/Struct/CloudSession.js with this:

connection.on("message", function (chunks) {
  let chunksArr = chunks.split("\n");
  for (let i = 0; i < chunksArr.length; i++) {
    let chunk = chunksArr[i];
    if (!chunk) {
      continue;
    }
    let json = JSON.parse(chunk);

    _this._client._debugLog("CloudData: Received message: " + json);

    if (json.method === "set") {
      _this._variables[json.name]  = new CloudVariable(_this._client, {
        name: json.name,
        value: json.value
      });

      _this.emit("set", _this._variables[json.name]);
    } else {
      _this._client._debugLog("CloudData: Method not supported: " + json.method);
    }
  }
});
@qucchia qucchia linked a pull request Jun 28, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant