From cd9ff6c42737eb9cc06543bd891ee2061da27574 Mon Sep 17 00:00:00 2001 From: ROUDET Franck IMT/OLPS Date: Thu, 10 Nov 2016 15:49:55 +0100 Subject: [PATCH 1/6] websockets works on node > 6 --- resources/piNoLd.json | 2 +- servers/websockets.js | 54 +++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/resources/piNoLd.json b/resources/piNoLd.json index fc6f6ad..4fe0493 100644 --- a/resources/piNoLd.json +++ b/resources/piNoLd.json @@ -10,7 +10,7 @@ "customFields": { "hostname":"localhost", "port": 8484, - "secure": true, + "secure": false, "dataArraySize" : 30 }, "links": { diff --git a/servers/websockets.js b/servers/websockets.js index ad2a594..a18fcac 100755 --- a/servers/websockets.js +++ b/servers/websockets.js @@ -6,10 +6,26 @@ var WebSocketServer = require('ws').Server, /** * Fake Array.Observe if not available */ -var ArrayObserver = Array.observe || function (resource, callback, extra) { - resource.push = function (data) { - Array.prototype.push.call(resource, data); - callback([{object: resource}]); +if (Array.observe) { + function ArrayObserver(res, cb, methods) { + Array.observe(res.res.data,cb, methods); + }; +} else { + var callbacklist={'properties': {}, 'actions' : {} }; + function ArrayObserver(res, cb, methods) { + if (! callbacklist[res.type][res.resname]) { + callbacklist[res.type][res.resname]=[]; + } + callbacklist[res.type][res.resname].push(cb); + res.res.data.push = function (data) { + Array.prototype.push.call(res.res.data, data); + if (callbacklist[res.type][res.resname]) { + callbacklist[res.type][res.resname].forEach(function(cb) { + cb([{object: res.res.data}]); + }); + } + + }; }; }; @@ -21,14 +37,28 @@ exports.listen = function (server) { if (!utils.isTokenValid(reqUrl.query.token)) { ws.send(JSON.stringify({'error': 'Invalid access token.'})); } else { - try { - ArrayObserver(selectResource(reqUrl.pathname), function (changes) { //#C + var observedResource = selectResource(reqUrl.pathname); + var resObjserver = function (changes) { //#C + console.log("coucou"); ws.send(JSON.stringify(changes[0].object[changes[0].object.length - 1]), function () { }); - }, ['add']) + }; + try { + ArrayObserver(observedResource, resObjserver, ['add']); } catch (e) { //#D console.log('Unable to observe %s resource!', url); } + ws.on('close', function (code, message) { + if (! Array.observe) { + var array = callbacklist[observedResource.type][observedResource.resname] + var index = array.indexOf(resObjserver); + if (index > -1) { + array.splice(index, 1); + } + } + console.log("ws connection is closed"); + }); + } }); }; @@ -38,14 +68,14 @@ function selectResource(url) { //#E parts.shift(); var result; - console.log(" parts ==> ", parts) + //console.log(" parts ==> ", parts) if (parts[0] === 'actions') { - result = resources.links.actions.resources[parts[1]].data; + result = resources.links.actions.resources[parts[1]]; } else { - result = resources.links.properties.resources[parts[1]].data; + result = resources.links.properties.resources[parts[1]]; } - console.log(result); - return result; + // console.log("===> ", result); + return { res: result, type: parts[0], resname: parts[1]}; } //#A Create a WebSocket server by passing it the Express server From 0a357aec5bf84d2d7d696b6516daf26122a1a083 Mon Sep 17 00:00:00 2001 From: ROUDET Franck IMT/OLPS Date: Mon, 21 Nov 2016 10:24:59 +0100 Subject: [PATCH 2/6] remove weird log --- servers/websockets.js | 1 - 1 file changed, 1 deletion(-) diff --git a/servers/websockets.js b/servers/websockets.js index a18fcac..de76e64 100755 --- a/servers/websockets.js +++ b/servers/websockets.js @@ -39,7 +39,6 @@ exports.listen = function (server) { } else { var observedResource = selectResource(reqUrl.pathname); var resObjserver = function (changes) { //#C - console.log("coucou"); ws.send(JSON.stringify(changes[0].object[changes[0].object.length - 1]), function () { }); }; From 1f2c7463282ee2e40a98d169751fb583c80ace0b Mon Sep 17 00:00:00 2001 From: franckOL Date: Mon, 21 Nov 2016 15:16:40 +0100 Subject: [PATCH 3/6] add .project to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 133fb42..64a6827 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# IDE +.project + # Logs logs *.log From 278ac122c97539e20474a7196c5f2cefe66cfd54 Mon Sep 17 00:00:00 2001 From: franckOL Date: Mon, 28 Nov 2016 14:29:06 +0100 Subject: [PATCH 4/6] Compatible with node 4 --- servers/websockets.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/servers/websockets.js b/servers/websockets.js index de76e64..bb543ee 100755 --- a/servers/websockets.js +++ b/servers/websockets.js @@ -7,12 +7,12 @@ var WebSocketServer = require('ws').Server, * Fake Array.Observe if not available */ if (Array.observe) { - function ArrayObserver(res, cb, methods) { + var ArrayObserver = function (res, cb, methods) { Array.observe(res.res.data,cb, methods); }; } else { var callbacklist={'properties': {}, 'actions' : {} }; - function ArrayObserver(res, cb, methods) { + var ArrayObserver= function (res, cb, methods) { if (! callbacklist[res.type][res.resname]) { callbacklist[res.type][res.resname]=[]; } From 72320750c049a3e3bd2dafeed32f2a3d0743bede Mon Sep 17 00:00:00 2001 From: Dominique Guinard Date: Sun, 11 Dec 2016 16:24:34 +0000 Subject: [PATCH 5/6] Making secure the default --- resources/piNoLd.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/piNoLd.json b/resources/piNoLd.json index 4fe0493..fc6f6ad 100644 --- a/resources/piNoLd.json +++ b/resources/piNoLd.json @@ -10,7 +10,7 @@ "customFields": { "hostname":"localhost", "port": 8484, - "secure": false, + "secure": true, "dataArraySize" : 30 }, "links": { From abf512b097808f09252c225c88192874b5100504 Mon Sep 17 00:00:00 2001 From: Franck Roudet Date: Mon, 21 Aug 2017 16:19:12 +0200 Subject: [PATCH 6/6] Supress coap and dht dependencies --- package.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/package.json b/package.json index 19df7a8..7eb7b40 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "dependencies": { "bl": "^1.0.0", "body-parser": "^1.13.1", - "coap": "^0.13.1", "consolidate": "^0.13.1", "cors": "^2.7.1", "crypto": "0.0.3", @@ -62,8 +61,6 @@ "ws": "^1.0.1" }, "optionalDependencies": { - "onoff": "^1.0.4", - "node-dht-sensor": "^0.0.8" }, "devDependencies": { "assert": "^1.3.0",