Problem using WebSockets on ESP8266 and Espruino

Posted on
  • I am trying to follow this example to setup a sample Websocket Client but the client won't connect nor does it thrown an error. None of the callback functions are ever called.

    I made sure that the WiFi is connected before the Websocket is initialized. The server doesn't see that the client running on Espruino on an ESP8266 is even connected. Does the built-in Websocket support the built in @require("Wifi") model?

    This same code runs fine when run from the Mac console when the websocket client is running inside the Mac and the server is also on the Mac.

    I have verified that the HTTP object is able to call another Web-server on the Mac.. So its definitely not a connectivity issue.

    Just need to know if the WebSocket object knows to utilize the connection as established by the Wifi.connect() API.

      var host = "ws://192.168.1.101:8080";
      var ws = new WebSocket(host);
    
      ws.on('open', function() {
        console.log("Connected to server");
        setTimeout(saySomething, 5000);
      });
    
      ws.on('message', function(msg) {
        console.log("MSG: " + msg);
        setTimeout(saySomething, 5000);
    
      });
    
      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);
      });
    
      function saySomething() {
        ws.send("{'test':'123'}");
      }
    
  • Your code above is not like the client here - the port is passed as an option

    http://www.espruino.com/ws

    The client assumes there is a server on that ip and port - what do you have running as a server?

  • I tried the example but that didn't work either. Infact the code didn't work on the desktop either.. When running like a Websocket client . I was getting a "invalid url" error on the desktop.

    I changed the code to what it is now and atleast it began working with a WebSocket server (code below) and the WebSocket client when both are running on the desktop

    var WebSocketServer = require('ws').Server,
        wss = new WebSocketServer({
            port: 8080
        });
    
    wss.on('connection', function connection(ws) {
        ws.room = [];
        ws.send("User Joined");
    
        ws.on('message', function(message) {
    
    
            console.log("Msg from client - " + message);
            ws.send("Echo from Server " + message);
          
        });
    
        ws.on('error', function(er) {
            console.log(er);
        })
    
    
        ws.on('close', function() {
            console.log('Connection closed')
        })
    });
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Problem using WebSockets on ESP8266 and Espruino

Posted by Avatar for swaroop @swaroop

Actions