You are reading a single comment by @user65754 and its replies. Click here to read the full conversation.
  • Hi.

    I tried that code, but i just recieve unreadyable data at the server.

    ws.on('message',function(msg) { print("[WS] "+JSON.stringify(msg)); });
    

    The result in the web-ide is like that:

    [WS] "\xC7\xD3\x00\x00\xC7\xD3\x00\x00\xC7\xD3"
    [WS] ""
    [WS] "\x1A\t\x00\x00\x1A\t\x00\x00\x1A\t"
    [WS] ""
    [WS] ""
    [WS] "/<\x00\x00/<\x00\x00/<"
    [WS] "bl\x1C\x00bl\x1C\x00bl"
    [WS] "R\x9E\xEF\x00R\x9E\xEF\x00R\x9E"
    [WS] "<C\x00\x00<C\x00\x00<C"
    [WS] "\xE9\xBC\x00\x00\xE9\xBC\x00\x00\xE9\xBC"
    

    That should be always the same message...

    ws.send("hallo welt");
    

    What is wrong with the code? I run it with the pico VERSION 1v85 in combination with the esp8266.
    I hope someone can help me with that.

    In addition the connection interrupt all the time. Is there an easy solution for a stable connection? I want to send a lot of data from client to the server.

    FullCode:

    Serial2.setup(9600, {    rx: A3,    tx: A2}); 
    var wifi = require("ESP8266WiFi").connect(Serial2, function(err) {
        if (err) throw err;
        wifi.reset(function(err) {
            if (err) throw err;
            console.log("Connecting to WiFi");
            wifi.connect("SSID", "12345", function(err) {
                if (err) throw err;
    
                var server = require('ws').createServer(onPageRequest);
                server.listen(8000);
                server.on("websocket", function(ws) {
                    ws.on('message',function(msg) {
                      print("[WS] "+JSON.stringify(msg));
                    });
                    ws.on('open', function() {
                      ws.send("Connected to server!");
                    });
                    ws.send("Hello from Espruino!");
                });
            });
        });
    });
    var page = '<html><head><meta charset="utf-8"><title>WebSocket Client Test...</title></head><body><script>var ws;setTimeout(function(){';
    page += 'ws = new WebSocket("ws://" + location.host + "/my_websocket", "text");';
    page += 'ws.onmessage = function (event) { console.log("MSG: "+event.data); };';
    page += 'setTimeout(function() { ws.send("Hello to Espruino!"); }, 1000);';
    page += '},1000);</script></body></html>';
    page += '</script></body></html>';
    function onPageRequest(req, res) {
        res.writeHead(200, {        'Content-Type': 'text/html'    });
        res.end(page);
    }
    

    In Browserconsole (Chrome) i entered "ws.send(hallo welt)".

    Thanks.

About

Avatar for user65754 @user65754 started