silvrgit/app/js/tempSocket.js
martind2000 3e1087fd70 init
2016-03-08 21:52:21 +00:00

48 lines
1.1 KiB
JavaScript

(function () {
console.log('Starting socket?');
var url = "ws://api.silvrtree.co.uk:8039";
var wsCtor = window['MozWebSocket'] ? MozWebSocket : WebSocket;
this.socket = new wsCtor(url, 'stream');
this.handleData = function(d) {
switch(d.id) {
case 'LightingDataReceived':
// this.updateLighting(d.sensorData.d);
break;
case 'ProjectorDataReceived':
// this.updateProj(d.sensorData.d);
break;
case 'HeatingDataReceived':
break;
default:
}
};
this.handleWebsocketMessage = function (message) {
try {
var command = JSON.parse(message.data);
}
catch (e) { /* do nothing */
}
if (command) {
//this.dispatchCommand(command);
this.handleData(command);
}
};
this.handleWebsocketClose = function () {
alert("WebSocket Connection Closed.");
};
this.socket.onmessage = this.handleWebsocketMessage.bind(this);
this.socket.onclose = this.handleWebsocketClose.bind(this);
})();