websocket example connection error

Posted on
  • when attempting to follow the websocket client example at http://www.espruino.com/ws, i get errors on the server (running node.js on a windows10 laptop) as:

    RangeError: Invalid WebSocket frame: RSV1 must be clear

    When I modify the code to use the npm ws client on the laptop, it works fine.

    server:
    node 8.9.4,
    "dependencies": {

    "express": "^4.16.3",
    "http": "0.0.0",
    "url": "^0.11.0",
    "ws": "^5.1.1"
    

    }

    espruino wifi client:
    1v97

    function wsConnect() {
        ws = new WebSocket(host, {path: '/', perMessageDeflate: false, port: 8080});
        ws.on('open', function() {
          // For WebSocket clients only
          console.log("Connected to server");
        });
    
        ws.on('message', function(msg) {
          console.log("MSG: " + msg);
        });
    
        ws.on('close', function() {
          console.log("Connection closed");
        });
    
        ws.on('handshake', function() {
          console.log("Handshake Success");
        });
    
        ws.on('ping', function() {
          console.log("Got a ping");
        });
    
        ws.on('pong', function() {
          console.log("Got a pong");
        });
    
        ws.on('rawData', function(msg) {
          console.log("RAW: " + msg);
        });
    }
    
    

    output on the repl is

    wsConnect()
    =undefined
    RAW: HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: j5MohIRvl3Pm9AVPI+aTUTQIKxI=
    Handshake Success
    Connected to server
    RAW: something
    MSG: something
    RAW: something
    MSG: something
    Connection closed
    Connection closed

  • i've tested the server with versions of npm ws 5, 4, 3, and 2. all give the same error about RSV1 must be clear, after connecting from the client. i have not tested the client on espruino wifi with versions older than 1v97.

  • Thanks for letting me know. Sorry about this - it seems to be a bug introduced in a recent pull request to Espruino. I'll try and get it fixed as soon as possible

    If you try firmware 1v96 (or any commit before 46e4bc69d73631b63f62b11febb4f2c07ec1acf3­) then that should work.

    You can follow the fix here if you're interested: https://github.com/espruino/Espruino/iss­ues/1405

  • thanks @Gordon. conversely, when trying to run a websocket server on the espruino, and connect from a node.js client, i get the error:

    Error: Server sent a subprotocol but none was requested

    and the client disconnects. i will try 1v96 today.

  • i have confirmed that 1v96 works with espruino as the websocket client. i am still receiving an error when espruino is the server, using the example code, and i use this code in node.js

    Error: Server sent a subprotocol but none was requested

    const WebSocket = require('ws');
     
    const ws = new WebSocket('ws://192.168.1.203:8000/', {
        protocol : "echo-protocol",
        protocolVersion: 13 });
     
    ws.on('open', function open() {
      ws.send('something');
    });
    
    ws.on('error', function(err) {
        console.log(err);
    })
     
    ws.on('message', function incoming(data) {
      console.log(data);
    });
    
  • And the error comes from Node.js?

    Strange thing is it'd seem that you are requesting a subprotocol of "echo-protocol", unless I'm misunderstanding?

  • This may be user error, as the espruino websocket server example works when the client is a web browser. when trying to get the client to be a node.js app, i'm having issues.

    this gives the same error from node.js. i can see the blue light flash on the espruino when i run the client, but there is no output in the espruino repl.

    is there an example for espruino websocket server, and node.js client?

    const WebSocket = require('ws');
     
    const ws = new WebSocket('ws://192.168.1.203:8000/');
     
    ws.on('open', function open() {
      ws.send('something');
    });
    
    ws.on('error', function(err) {
        console.log(err);
    })
     
    ws.on('message', function incoming(data) {
      console.log(data);
    });
    
  • changing the connection to mirror the browser client connection in the embedded web page worked

    const ws = new WebSocket('ws://192.168.1.203:8000', 'protocolOne');
    

    i didn't see that 'protocolOne' parameter documented anywhere. it works, so i'll move on for now.

  • Just to add that @opichals has just fixed this, so any new firmware from http://www.espruino.com/binaries/travis/­master/ (or 1v98 and later) will have this fixed.

  • there were other regressions with 1v97.60, e.g. the getPage() example did not return a page. reverting to 1v97 or 1v96 does work. i'll wait for an official release to move forward from 1v96 for now.

  • i tried 1v97.62 as well. this code does not return a page to the repl anymore

    function getPage() {
      require("http").get("http://www.pur3.co.­uk/hello.txt", function(res) {
        console.log("Response: ",res);
        res.on('data', function(d) {
          console.log("--->"+d);
        });
      });
    }
    
    
  • also with 1v97.62 i am not able to complete a websocket connection with espruino as the server and a webpage as the client. it stays in the connecting state. same code is working in 1v96

  • Thanks - I think @opichals is looking into this now :)

  • I tried the getPage() and it did work for me when the URL is correct. Cutting & pasting from the forum's example however mangled the URL for me so I got a 'Not found' error.

    @J{a}SON could you try this and post the whole console result?

    require("http").get("http://www.pur3.co.­uk/hello.txt", function(res) {
      console.log("Response: ",res);
      res.on('data', function(d) {
        console.log("--->"+d);
      });
    }).on('error', function(err) {
      console.log('ERROR', err);
    });
    
  • which build are you looking at? the larger remaining problem is the espruino not working as a websocket server, as documented in the github issues https://github.com/espruino/Espruino/iss­ues/1405.

    i'll test again if you let me know which build to look at.

  • @J{a}SON I would wait for the https://github.com/espruino/Espruino/pul­l/1415 to get merged and then grab the master build for further tests.

  • Just done :)

  • ok, this is working now with espruino as the ws client, and ws server. getPage() is working as well.

    i do have an additional question: when espruino is the ws client, and i send a message from the node.js server, it shows up as a rawData event as well as a message event. is this expected?

    wsConnect()
    =undefined
    RAW: HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: NRGmTTLwn7B71D1ZNRdkobCXs/0=
    Sec-WebSocket-Protocol: echo-protocol
    Handshake Success
    Connected to server
    RAW: something
    MSG: something

  • Yes. Listen to one event or the other...

  • Just to add - 'rawData' is basically just a debugging aid for the websocket implementation. 99.9% of the time you'd want to use the message event :)

  • glad we got this sorted out. now i can move on to programming ble on my pucks :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

websocket example connection error

Posted by Avatar for J{a}SON @J{a}SON

Actions