From 77b459105482f540e7100cd10e951ec839d4d7a7 Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Wed, 17 Feb 2016 11:52:09 +0000 Subject: [PATCH] websocket stuff --- .idea/workspace.xml | 77 ++++++++++++++++++++++++++++----------------- routes/temp.js | 4 +-- views/temp.ejs | 53 +++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 31 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index df6698a..cf9e731 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,8 +3,8 @@ - + @@ -28,8 +28,8 @@ - - + + @@ -48,8 +48,8 @@ - - + + @@ -462,18 +462,6 @@ @@ -751,7 +769,7 @@ - + @@ -793,6 +811,7 @@ + @@ -1183,14 +1202,6 @@ - - - - - - - - @@ -1199,10 +1210,18 @@ + + + + + + + + - - + + diff --git a/routes/temp.js b/routes/temp.js index 77b7a24..98b221b 100644 --- a/routes/temp.js +++ b/routes/temp.js @@ -174,7 +174,7 @@ wsServer.on('request', function (request) { return; } - var connection = request.accept('', request.origin); + var connection = request.accept('stream', request.origin); logger.debug((new Date()) + ' Connection accepted.'); var sendSocketHandler = (obj) => { @@ -199,4 +199,4 @@ wsServer.on('request', function (request) { logger.debug((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); busEmitter.removeListener('sendSocket', sendSocketHandler); }); -});; +});;; diff --git a/views/temp.ejs b/views/temp.ejs index d887fad..255e27f 100644 --- a/views/temp.ejs +++ b/views/temp.ejs @@ -95,6 +95,59 @@ } ); + + + + + + (function () { + console.log('Starting socket?'); + var url = "ws://api.silvrtree.co.uk:8085"; + + 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); + })(); + + +