Avatar for Injecteer

Injecteer

Member since Aug 2016 • Last active May 2017
  • 5 conversations
  • 23 comments

Most recent activity

  • in JavaScript
    Avatar for Injecteer

    I have a NodeMCU dev board with ESP8266 MOD (?) chip on it. Is it compatible with EspruinoWIFI?

  • in JavaScript
    Avatar for Injecteer

    If I try connecting to wss://myserver.com:8822:

    var ws = require( 'ws' )( 'beta.mozaiq.io', { port:8822 } )
    

    I get

    console.log( ws.lastData )
    HTTP/1.1 400 Bad Request
    Server: nginx/1.10.1
    Date: Mon, 13 Feb 2017 11:35:49 GMT
    Content-Type: text/html
    Content-Length: 271
    Connection: close
    400 The plain HTTP request was sent to HTTPS port

    In the chrome ws-client the connection over wss:// is working.

    I have another SSL-free ws-endpoint on 8827 on my server, and it's working just fine in ESP.

  • in JavaScript
    Avatar for Injecteer

    I'd like to use the wss for my client on ESP8266, but the require( 'tls' ) module can not be found...

  • in JavaScript
    Avatar for Injecteer

    Yep, I had a modified copy of old handshake() code with hard-coded Sec-WebSocket-Key instead of new this.key.source. The result was, that no open event was emitted. After I poured the new crypto-aware code in, the problem disappeared.

    I created a pull-request. Please see https://github.com/espruino/EspruinoDocs­/pull/289. It shouldn't have any impact on space requirement for the library.

  • in JavaScript
    Avatar for Injecteer

    @Gordon The code is not about "my testing" :), it provides a possibility to inject optional headers (like auth) into the Websocket's handshake - lines 31-34.

    As of now this is possible only if you override the default handshake() method, but as my case shows, it might break some things up (like crypto key gets lost).

    I'm using ESP8266 indeed, and the handshake code I provided works just fine with crypto and optional request headers.

  • in JavaScript
    Avatar for Injecteer

    Ok, I nailed the problem. My code doesn't work with the latest remote version of ws module -> http://www.espruino.com/modules/ws.js.

    Luckily I have a local (slightly modified) version of the ws-module (without the crypto module), and with this one the code from above works as before!

    Gordon, can you please modify the following lines in the ws module?

    function WebSocket(host, options) {
      this.socket = null;
      options = options || {};
      this.host = host;
      this.port = options.port || 80;
      this.protocolVersion = options.protocolVersion || 13;
      this.origin = options.origin || 'Espruino';
      this.keepAlive = options.keepAlive * 1000 || 60000;
      this.masking = options.masking!==undefined ? options.masking : true;
      this.path = options.path || "/";
      this.protocol = options.protocol;
      this.lastData = "";
      this.key = buildKey();
      this.connected = false || options.connected;
      this.headers = options.headers || {};
    }
    
    WebSocket.prototype.handshake = function () {
      var socketHeader = [
        "GET " + this.path + " HTTP/1.1",
        "Host: " + this.host,
        "Upgrade: websocket",
        "Connection: Upgrade",
        "Sec-WebSocket-Key: " + this.key.source,
        "Sec-WebSocket-Version: " + this.protocolVersion,
        "Origin: " + this.origin
      ];
      if (this.protocol)
        socketHeader.push("Sec-WebSocket-Protoco­l: "+this.protocol);
      
      for( var key in this.headers ){
        if( this.headers.hasOwnProperty( key ) )
          socketHeader.push( key + ": " + this.headers[ key ] );
      }
    
      this.socket.write(socketHeader.join("\r\­n")+"\r\n\r\n");
    };
    

    so that I can further test my stuff the new ws code

  • in JavaScript
    Avatar for Injecteer

    I'm facing a strange situation: after changing the code, the event listeners are not registered anymore.

    The code now looks like:

    MyClass.prototype.initialize = function () {
        var self = this;
        self.wifi.connect( self.ssid, { password:self.wlanPw }, function( err ){
          console.log( "connected? info=", self.wifi.getIP() );
          if( !err ) self.connect();
        } );
    }
    
    MyClass.prototype.connect = function () {
    	this.ws = new WS( this.serverHost, { port:8822 } );
    	this.ws.gwId = this.wifi.getIP().mac;
    	this.ws.premiseId = this.premiseId;
    	this.ws.handshake = this.handshake.bind( this.ws );
    	this.ws.on( 'open', function(){
    	  console.log( 'ws openned' );
    	} );
    	this.ws.on( 'message', this.onWsMessage );
    	this.ws.on( 'close', this.onWsClose );
    
    	console.log( "Connecting to ", this.serverHost, 8822 );
    };
    
    MyClass.prototype.onWsMessage = function( msg ) { doSomething(); };
    MyClass.prototype.onWsClose = function() { ... };
    

    in the console I see

    connected? info= {
    "ip": "192.168.0.16",
    "netmask": "255.255.255.0",
    "gw": "192.168.0.1",
    "mac": "5c:cf:7f:19:71:73"
    }
    Connecting to ec2-52-34-212-117.us-west-2.compute.amaz­onaws.com 8822

    I see also, that the WS request hits the server.

    It used to work, so now I'm banging my head trying to figure out what could possibly go wrong here...

  • in Projects
    Avatar for Injecteer

    I'm getting

    ERROR: Out of Memory!
    Uncaught Error: Function "connect" not found!
    at line 1 col 108
    ...7a9f8486ea8bf8d406696.js").connect( "someSSID", { password:"AA...

Actions