0

I'm struggling to understand what's happening here...

I have a websocket server on 192.168.1.64:81

I need to send data to the socket from a web page using this javascript:

window.onload = function() {
    var connection = new WebSocket("ws://"+location.hostname+":81", ['arduino']);
    connection.onopen = function() {
        connection.send('Connect ' + new Date());
    };
    connection.onerror = function(error) {
        console.log('WebSocket Error ', error);
    };
    connection.onmessage = function(e) {
        console.log('Server: ', e.data);
    };

    function sendData() {
        var data="#"+joystick.deltaX()+","+joystick.deltaY();
        connection.send(data);
    }};

Now this is what happens: if I open the js console (in firefox) I see the "connection is undefined" error... but if I copy-paste to the console the line:

    var connection = new WebSocket("ws://"+location.hostname+":81", ['arduino']);

the socket gets defined correctly and the updater begins streaming data correctly through the socket!!

What am I missing? Should I be aware of some well known issue?

1 Answer 1

1

The following functions use the connection variable, but the variable is out of scope, because connection is defined using the var (local) keyword:

connection.onopen = function() {
    connection.send('Connect ' + new Date());
};
function sendData() {
    var data="#"+joystick.deltaX()+","+joystick.deltaY();
    connection.send(data);
}};

Either define `connection as a global value:

connection = new WebSocket("ws://"+location.hostname+":81", ['arduino']);

Or use an internal binding / reference:

connection.onopen = function(e) {
    e.target.send('Connect ' + new Date());
};
sendData = function() {
    var data="#"+joystick.deltaX()+","+joystick.deltaY();
    this.send(data);
}.bind(connection);

Otherwise initialize connection as a global and assign onload like this:

    var connection;
window.onload = function() {
        connection = new WebSocket("ws://"+location.hostname+":81", ['arduino']);
        connection.onopen = function() {
            connection.send('Connect ' + new Date());
        };
        connection.onerror = function(error) {
            console.log('WebSocket Error ', error);
        };
        connection.onmessage = function(e) {
            console.log('Server: ', e.data);
};};
Sign up to request clarification or add additional context in comments.

3 Comments

I tried the first option before but looks like the server doesn't like to load html and receive data on port 81 at the same time. I'm now trying to define ''var connection;'' as a global and then defining when .onload is complete! Testing it now!
If your server doesn't allow for concurrent connections, that's a bigger problem, since the WebSockets connection will block the device. Consider using an evented server library for Arduino. Maybe facil.io would work on an embedded device (I'm biased), or (if using JavaScript on the server) consider Node.js.
the .onload trick works fine for me but I guess what you are saying is true. It was just a misplaced variable init, the .onload idea works fine though! thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.