Skip to content

Send request via websocket #288

@mtexier

Description

@mtexier

Hi,

I would like to use the websocket API, to send a request to a websocket server to request a data.
I tried to adapt the getXhtr example, but it does not seems to work well: I receive the data but Blockly is blocked forever.

Here is my code:

    var wrapper = function sensor(command, callback) {
       Code.socket.addEventListener("message", (e) => {
          callback(e.data);
      });
      Code.socket.send('{ "command" : "READ" , "params": ["' + command + '"]}');
    };
    interpreter.setProperty(globalObject, 'sensor',
        interpreter.createAsyncFunction(wrapper));

The eventListener seems to be called, but the sensor function does not return in the interpreter (adding a console.log before callback print the right value).
Also, always adding a new eventlistener seems to not be very clean.

Does someone have tried websocket? How can I do?
Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions