(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); })();