You are reading a single comment by @Wilberforce and its replies. Click here to read the full conversation.
  • I've written a crypto module to use on boards without the crypto module in firmware - it implements the SHA1 used by the web sockets module. The ws module does not need to be modified.

    For testing if the crypto.js file is copied down to a web ide project it can be tested.

    The following code assumes the ESP8266 has a wifi.save and is connected to a network.

    var page = '<!DOCTYPE html><html lang=en><head><meta charset=utf-8>';
    page += ' <meta http-equiv=X-UA-Compatible content="IE=edge">';
    page += '<meta name=viewport content="width=device-width,initial-scal­e=1">';
    page += '<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/boo­tstrap/3.3.5/css/bootstrap.min.css">';
    page += '</head><body>';
    page += '<div class="well well-lgt">';
    page += '<div class="panel-heading"><h3>Websockets</h3­></div>';
    page += '<button class="btn btn-warning" id="send" onClick="ws.send(count);count=count+1;th­is.innerHTML=\'Send \'+count">Send to Espruino</button>';
    page += '<ul id="list" class="list-group"></ul>';
    page += '</div>';
    page += '<script>';
    page += 'var ws,count=1;setTimeout(function(){';
    page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");';
    page += 'ws.onmessage = function (event) { console.log({msg:event.data});';
    page += 'var ul = document.getElementById("list");';
    page += 'var li = document.createElement("li");';
    page += 'li.className="list-group-item";';
    page += 'li.appendChild(document.createTextNode(­event.data));';
    page += 'ul.appendChild(li);';
    page += '};';
    page += 'setTimeout(function() { ws.send("Hello to Espruino!");ws.send(JSON.stringify({brow­ser:95})); }, 1000);';
    page += '},1000);</script></body></html>';
    function onPageRequest(req, res) {
      res.writeHead(200, {
        'Content-Type' : 'text/html'
      });
      res.end(page);
    }
    var server = require('ws').createServer(onPageRequest­);
    server.listen(80);
    server.on("websocket", function (ws) {
      ws.on('message', function (msg) {
        console.log({
          ws : msg
        });
        if (msg % 2 === 0)
          ws.send(msg + ' is multiple of two');
      });
      ws.send("Hello from Espruino!");
      ws.send(JSON.stringify({
          date : Date.now()
        }));
    
    });
    

    Start a browser at your Eps8266 address and see:

    If you click the button, you can see the event appear in the Web IDE console. Every 2nd click causes the server to send back to the browser.... two way socket!

    https://github.com/wilberforce/EspruinoD­ocs/blob/master/modules/crypto.js

    For clarity, this the code that gets sent to the browser, using a CDN to get the bootstrap css:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scal­e=1"><link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/boo­tstrap/3.3.5/css/bootstrap.min.css">
    </head>
       <body>
       	<div class="well well-lgt">
       		<div class="panel-heading"><h3>Websockets</h3­>
       		</div>
       	    <button class="btn btn-warning" id="send" onClick="ws.send(count);count=count+1;th­is.innerHTML='Send '+count">
    Send to Espruino</button>
       		<ul id="list" class="list-group">
       		</ul>
       	</div>
    <script>
    var ws, count = 1;
    setTimeout(function () {
      ws = new WebSocket("ws://" + location.host + "/my_websocket", "protocolOne");
      ws.onmessage = function (event) {
        console.log({
          msg : event.data
        });
        var ul = document.getElementById("list");
        var li = document.createElement("li");
        li.className = "list-group-item";
        li.appendChild(document.createTextNode(e­vent.data));
        ul.appendChild(li);
      };
      setTimeout(function () {
        ws.send("Hello to Espruino!");
        ws.send(JSON.stringify({
            browser : 95
          }));
      }, 1000);
    </script>
       </body>
    </html>
    

    2 Attachments

About

Avatar for Wilberforce @Wilberforce started