Websocket gets closed immediately

Posted on
  • Using the following code in Espruino Web IDE:

    var WebSocket = require("ws");
    var ws = new WebSocket( 'mydomain.com', { port:8822 } );
    
    ws.on('open', function() {
      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('rawData', function(msg) {
      console.log("RAW: " + msg);
    });
    
    ws.send( JSON.stringify( { text:'HHHHLLLLL' } ) );
    

    I can see in the server logs, that the WS-connection is established and then closes within some milisecs:

    12:24:06.009 [vert.x-eventloop-thread-0] INFO com.my.GatewayWSVerticle - registered WS for [11.111.111.111]
    12:24:06.220 [vert.x-eventloop-thread-0] INFO com.my.GatewayWSVerticle - unregistered WS for [11.111.111.111]
    
    

    If I enter ws.initializeConnection(); in the left-hand side of IDE, I'm getting the lines:

    RAW: HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
    Handshake Success
    Connection closed
    

    If I connect to the very same server from my browser or from a z-way server @ raspi, then it works like charm.

    Am I missing something or is it a WS-bug?

    TIA

  • What device are you running this on?

    It's possible that it's because you're using send right away. new WebSocket won't be blocking, so it's possible it's not even connected when you're trying to send.

  • Thanks for your reply Gordon!

    I'm on esp8266.

    I called ws.initializeConnection(); in the left console, after I sent the code to device. It brought me the following console out:

        RAW: HTTP/1.1 101 Switching Protocols
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=
        Handshake Success
        Connection closed
    

    so, the ws is in closed state immediately after initializing, and calling send() on a dead socket brings obviously nothing

  • Ahh, thanks. Just looked into this and yes, for some reason (i think it might have been one of my changes for the server), the websocket client got broken.

    I just fixed it, so if you upload now it should work.

  • The connection is established and remains open now, whoohoo!

    Although some other problems arose...

    If I call e.g. ws.send( JSON.stringify( { "type":'discoveryResponse', "data":[] } ) );, then the connection gets closed, and the server logs the following:

    14:47:53.714 [vert.x-eventloop-thread-0] INFO com.my.GatewayWSVerticle - registered WS for [11.111.111.111]
    14:48:26.090 [vert.x-eventloop-thread-0] ERROR i.vertx.core.net.impl.ConnectionBase - io.netty.handler.codec.CorruptedFrameException: unmasked client to server frame
    14:48:26.090 [vert.x-eventloop-thread-0] INFO com.my.GatewayWSVerticle - unregistered WS for [11.111.111.111]
    

    If I call ws.initializeConnection(); then I get to see the following in the logs:

    14:50:14.331 [vert.x-eventloop-thread-0] ERROR i.vertx.core.net.impl.ConnectionBase - io.netty.handler.codec.CorruptedFrameException: unmasked client to server frame
    14:50:14.331 [vert.x-eventloop-thread-0] ERROR i.vertx.core.net.impl.ConnectionBase - io.netty.handler.codec.DecoderException: io.netty.util.IllegalReferenceCountException: refCnt: 0
    
  • Hmm... What are you using as your WebSocket server? I was pretty sure masking was optional (so you may find it's a configuration option you can turn off in your server).

  • I'm using vertx.io. It seems to have masking on by default. here a discussion on masking

    btw, would the ws module work over the wss protocol?

  • You might still be able to tweak vertex.io or report it as a bug - after all, the pretty much standard ws library seems to work. Although the RFC asks for masking, dropping connections if it's not there is needlessly harsh, especially given the reasons for masking being turned on.

    I don't have time to fix this right now, but I made an issue for it here: https://github.com/espruino/EspruinoDocs/issues/268

    There's some info there if you want to get stuck in and fix it :)

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

Websocket gets closed immediately

Posted by Avatar for Injecteer @Injecteer

Actions