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?