Websocket server problems

Posted on
  • I'm trying to setup a websocket server on a ESP8266.

    I have wifi connection, http requests work, WS connection looks okay (right headers and chrome send the first frame).

    The problem I have is that it do not look like the event for "websocket" is triggered i.e. i dont ge the ws object and no data.

    I have looked at different examples on the net, but cant figure out what I do wrong so any suggestions are welcome.


    1 Attachment

  • Hi user72696,

    Thanks very much for this example, it's the first time I've seen this REST approach so I thought I give your code a try.

    It works. If I type "192.168.1.70:8080/hello" in the browser then Espruino says "Hello World". I didn't see any problem firing the websocket.

    My ESP8266 is already connected to wifi with wifi.save() and I don't have the servos and leds connected so I changed the code a bit..

    var wifi = require("Wifi");
    
    var page = '<html><body><script>var ws;setTimeout(function(){';
    page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");';
    page += 'ws.onmessage = function (event) { console.log("MSG:"+event.data); };';
    page += 'setTimeout(function() { ws.send("Hello to Espruino!"); }, 1000);';
    page += '},1000);</script></body></html>';
    
    //Write a response 200 with message on res
    var httpResp = (res, message) => {
      res.writeHead(200, { 'Content-Type': 'text/plain' });
      res.end(message);
      console.log(message);
    };
    
    //Parse get request path in the form of /action/port/value and return object
    var parseREST = (req) => {
      var RESTReq = {};
      var a = url.parse(req.url, true);
      var pathParts = a.pathname.split("/");
    
      if (pathParts.length == 2 && pathParts[1] === "") {
        RESTReq.action = "default";
      }
    
      if (pathParts.length > 1) {
        RESTReq.action = pathParts[1];
      }
    
      if (pathParts.length > 2) {
        RESTReq.port = pathParts[2];
      }
    
      if (pathParts.length > 3) {
        RESTReq.value = pathParts[3];
      }
    
      console.log(JSON.stringify(RESTReq));
      return RESTReq;
    };
    
    function onPageRequest(req, res) {
      //Path mapping to function for REST API
      var RESTReq = parseREST(req);
      if (RESTReq.action == "default") {
        httpResp(res, "BIPed REST API 0.1");
      } else if (RESTReq.action == "hello") {
        httpResp(res, "Hello World");
      } else if(RESTReq.action== "wstest") {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(page);
      } else if (RESTReq.action == "ledOn") {
    //    digitalWrite(D0, false);
        httpResp(res, "Red Led On");
      } else if (RESTReq.action == "ledOff") {
    //    digitalWrite(D0, true);
        httpResp(res, "Red Led Off");
      } else if (RESTReq.action == "wifi") {
        httpResp(res, JSON.stringify(wifi.getStatus()) + JSON.stringify(wifi.getIP()));
      } else if (RESTReq.action == "write") {
    //    digitalWrite(RESTReq.port, RESTReq.value);
        httpResp(res, JSON.stringify(RESTReq));
      } else if (RESTReq.action == "servo") {
        if (RESTReq.value == "test") {
      //    testServo(RESTReq.port);
        } else if (RESTReq.value > 0) {
      //    move(RESTReq.port, RESTReq.value / 100);
        } else {
      //    move(RESTReq.port, 0);
        }
        httpResp(res, JSON.stringify(RESTReq));
      } else {
        res.writeHead(404, { 'Content-Type': 'text/plain' });
        res.end(JSON.stringify(RESTReq));
      }
    }
    
    var server = require('ws');
    server.createServer(onPageRequest).listeĀ­n(8080);
    server.on("websocket", function (ws) {
      console.log("WS Connected");
      ws.send("Test");
      ws.on('message', (msg) => {
        print("Websocket " + JSON.stringify(msg));
      });
    });
    

    Also I got some weird "ACORN compiler.js" warnings?

  • Thank's for taking the time to test the code out. I will try your code and see if that works.

    But when you tested could you send and receive frames in the websocket?

    When I tested I also got a websocket connection but could not send and receive any messages.

    Regards
    Fredrik

  • No problem, yes I saw the messages sent and received in chrome and in espruino console log and I also looked in the chrome developer mode at the frames and data.

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

Websocket server problems

Posted by Avatar for Fredrik @Fredrik

Actions