mirror of
https://gitlab.silvrtree.co.uk/martind2000/censis-archive.git
synced 2025-02-04 08:50:13 +00:00
”2016-09-20”
This commit is contained in:
parent
cb7c38e6d4
commit
4d7ee60494
@ -19,7 +19,8 @@ var authentication = require('basic-authentication');
|
||||
var Events = require('events');
|
||||
var busEmitter = new Events.EventEmitter();
|
||||
|
||||
var SocketHandler = require('./lib/sockethandler');
|
||||
//var SocketHandler = require('./lib/sockethandler');
|
||||
var SocketHandler = require('./lib/wshandler');
|
||||
|
||||
// Var apn = require('apn');
|
||||
|
||||
@ -129,7 +130,7 @@ app.get('/graph', authentication(({
|
||||
file: 'htpasswd', // Path of file
|
||||
suppress: true // Suppress throwing Error if wrong user
|
||||
})), function(req, res) {
|
||||
res.render(graphFile,{delimiter: '^'});
|
||||
res.render(graphFile,{delimiter: '^', port:(process.env.VCAP_APP_PORT || 3011)});
|
||||
});
|
||||
|
||||
app.get('/meeting', function(req, res) {
|
||||
@ -140,7 +141,7 @@ app.get('/meeting', function(req, res) {
|
||||
|
||||
|
||||
|
||||
var webSocket = new SocketHandler(busEmitter);
|
||||
|
||||
|
||||
// Glue routes
|
||||
mdotApi(app);
|
||||
@ -159,9 +160,16 @@ app.get('*', function(req, res) {
|
||||
res.status(404).render('404',{delimiter: '^'});
|
||||
});
|
||||
|
||||
app.listen(port, function() {
|
||||
/*
|
||||
var server = app.listen(port, function() {
|
||||
logger.info('Express listening on ',host, port);
|
||||
|
||||
});
|
||||
*/
|
||||
|
||||
var server = http.createServer(app).listen(port, function() {
|
||||
console.log('New server listening on port ' + port);
|
||||
});
|
||||
|
||||
|
||||
var webSocket = new SocketHandler(busEmitter, server, host);
|
||||
|
@ -41,6 +41,7 @@ var SOCKETMANAGER = (function() {
|
||||
if (item.hasOwnProperty('tick')) {
|
||||
// Ticks are ignored
|
||||
} else {
|
||||
console.log('deviceid', item.data.deviceid);
|
||||
if (item.data.deviceid === this.listeningID) {
|
||||
console.log('Updated', item.data);
|
||||
this.set({data: item.data});
|
||||
|
@ -1,22 +1,25 @@
|
||||
var WEBSOCKET = function(model) {
|
||||
|
||||
var wsUrl = 'ws://localhost:3001';
|
||||
|
||||
if ('https:' === document.location.protocol) {
|
||||
wsUrl = 'wss://mdotserver.bluemix.net:3001';
|
||||
} else {
|
||||
wsUrl = 'ws://localhost:3001';
|
||||
}
|
||||
var wsUrl = 'ws://localhost';
|
||||
var wsPort = 3011;
|
||||
if ('https:' === document.location.protocol) {
|
||||
wsUrl = 'wss://mdotserver.bluemix.net';
|
||||
wsPort = '443';
|
||||
} else {
|
||||
//wsUrl = 'ws://localhost:3001';
|
||||
wsUrl = 'ws://mdotserver.bluemix.net';
|
||||
wsPort = '';
|
||||
}
|
||||
|
||||
this.socket = null;
|
||||
this.timer = 0;
|
||||
|
||||
this.startWebSocket = function() {
|
||||
'use strict';
|
||||
console.log('Starting socket?');
|
||||
|
||||
var url = wsUrl;
|
||||
|
||||
var url = (wsPort === '') ? wsUrl : wsUrl + ':' + wsPort;
|
||||
console.log('Starting socket', url);
|
||||
var wsCtor = window['MozWebSocket'] ? MozWebSocket : WebSocket;
|
||||
this.socket = new wsCtor(url, 'stream');
|
||||
|
||||
@ -43,12 +46,13 @@ var WEBSOCKET = function(model) {
|
||||
|
||||
this.handleWebsocketMessage = function(message) {
|
||||
var command;
|
||||
|
||||
try {
|
||||
command = JSON.parse(message.data);
|
||||
}
|
||||
catch (e) { /* Do nothing */
|
||||
}
|
||||
|
||||
console.log(command);
|
||||
if (command) {
|
||||
this.handleData.call(this,command);
|
||||
}
|
||||
|
@ -14,17 +14,20 @@ var logger = require('log4js').getLogger();
|
||||
|
||||
var Sugar = require('sugar-date');
|
||||
|
||||
var server = http.createServer(function(request, response) {
|
||||
/*Var server;
|
||||
|
||||
server = http.createServer(function(request, response) {
|
||||
logger.info((new Date()) + ' Received request for ' + request.url);
|
||||
response.writeHead(404);
|
||||
response.end();
|
||||
});
|
||||
|
||||
server.listen(3001, function() {
|
||||
logger.info('Server is listening on port 3001');
|
||||
});
|
||||
server.listen(4443, function() {
|
||||
logger.info('Server is listening on port 4443');
|
||||
});*/
|
||||
|
||||
var wsServer = new WebSocketServer({
|
||||
/*
|
||||
Var wsServer = new WebSocketServer({
|
||||
httpServer: server, // You should not use autoAcceptConnections for production
|
||||
// applications, as it defeats all standard cross-origin protection
|
||||
// facilities built into the protocol and the browser. You should
|
||||
@ -32,10 +35,24 @@ var wsServer = new WebSocketServer({
|
||||
// to accept it.
|
||||
autoAcceptConnections: false
|
||||
});
|
||||
*/
|
||||
|
||||
module.exports = function(events) {
|
||||
var wsServer;
|
||||
|
||||
module.exports = function(events, server) {
|
||||
'use strict';
|
||||
logger.debug('creating with events', events);
|
||||
|
||||
const port = (process.env.VCAP_APP_PORT || 3011);
|
||||
wsServer = new WebSocketServer({
|
||||
httpServer: server, // You should not use autoAcceptConnections for production
|
||||
// applications, as it defeats all standard cross-origin protection
|
||||
// facilities built into the protocol and the browser. You should
|
||||
// *always* verify the connection's origin and decide whether or not
|
||||
// to accept it.
|
||||
autoAcceptConnections: false,
|
||||
port: port
|
||||
});
|
||||
|
||||
|
||||
function originIsAllowed(origin) {
|
||||
// Put logic here to detect whether the specified origin is allowed.
|
||||
@ -55,19 +72,19 @@ module.exports = function(events) {
|
||||
logger.info((new Date()) + ' Connection accepted.');
|
||||
|
||||
var sendSocketHandler = (obj) => {
|
||||
logger.debug('sendSocketHandler', obj);
|
||||
try {
|
||||
connection.sendUTF(JSON.stringify(obj));
|
||||
}
|
||||
catch (err) {
|
||||
logger.error(err);
|
||||
logger.warn('Offending object: ', obj);
|
||||
}
|
||||
};
|
||||
logger.debug('sendSocketHandler', obj);
|
||||
try {
|
||||
connection.sendUTF(JSON.stringify(obj));
|
||||
}
|
||||
catch (err) {
|
||||
logger.error(err);
|
||||
logger.warn('Offending object: ', obj);
|
||||
}
|
||||
};
|
||||
|
||||
events.on('sendSocket', sendSocketHandler);
|
||||
/*
|
||||
events.on('sendSocket', function(o){
|
||||
/*
|
||||
Events.on('sendSocket', function(o){
|
||||
logger.debug('sendSocket!!', o);
|
||||
});
|
||||
*/
|
||||
@ -82,10 +99,10 @@ module.exports = function(events) {
|
||||
}
|
||||
});
|
||||
|
||||
connection.on('close', function(reasonCode, description) {
|
||||
logger.info((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
|
||||
events.removeListener('sendSocket', sendSocketHandler);
|
||||
});
|
||||
connection.on('close', function(reasonCode, description) {
|
||||
logger.info((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
|
||||
events.removeListener('sendSocket', sendSocketHandler);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
69
mdot/mDotServer.censis/mDotServer.censis/lib/wshandler.js
Normal file
69
mdot/mDotServer.censis/mDotServer.censis/lib/wshandler.js
Normal file
@ -0,0 +1,69 @@
|
||||
/**
|
||||
*
|
||||
* User: Martin Donnelly
|
||||
* Date: 2016-09-07
|
||||
* Time: 15:33
|
||||
*
|
||||
*/
|
||||
|
||||
var WebSocketServer = require('ws').Server;
|
||||
var logger = require('log4js').getLogger();
|
||||
|
||||
var Sugar = require('sugar-date');
|
||||
|
||||
|
||||
var wsServer;
|
||||
|
||||
module.exports = function(events, server, host) {
|
||||
'use strict';
|
||||
|
||||
var port = 3011;
|
||||
|
||||
//wsServer = new WebSocketServer({server:server, port:4443});
|
||||
wsServer = new WebSocketServer({server:server, url: host});
|
||||
|
||||
|
||||
function originIsAllowed(origin) {
|
||||
// Put logic here to detect whether the specified origin is allowed.
|
||||
return true;
|
||||
}
|
||||
wsServer.on('connection', function connection(ws) {
|
||||
|
||||
logger.info('Creating event for this connection');
|
||||
|
||||
logger.info((new Date()) + ' Connection accepted.');
|
||||
|
||||
var sendSocketHandler = (obj) => {
|
||||
logger.debug('sendSocketHandler', obj);
|
||||
try {
|
||||
ws.send(JSON.stringify(obj));
|
||||
}
|
||||
catch (err) {
|
||||
logger.error(err);
|
||||
logger.warn('Offending object: ', obj);
|
||||
}
|
||||
};
|
||||
|
||||
events.on('sendSocket', sendSocketHandler);
|
||||
|
||||
ws.on('message', function(message) {
|
||||
if (message.type === 'utf8') {
|
||||
logger.info('Received Message: ' + message.utf8Data);
|
||||
ws.send(message.utf8Data);
|
||||
} else if (message.type === 'binary') {
|
||||
logger.info('Received Binary Message of ' + message.binaryData.length + ' bytes');
|
||||
ws.send(message.binaryData);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', function(reasonCode, description) {
|
||||
logger.info((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
|
||||
events.removeListener('sendSocket', sendSocketHandler);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
return module;
|
||||
};
|
@ -32,7 +32,8 @@
|
||||
"request-promise": "^4.1.1",
|
||||
"sugar-date": "^2.0.0",
|
||||
"uuid-pure": "^1.0.10",
|
||||
"websocket": "^1.0.23"
|
||||
"websocket": "^1.0.23",
|
||||
"ws": "^1.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"after": "^0.8.1",
|
||||
|
@ -218,6 +218,11 @@
|
||||
style="background-color: darkred;color: white;font-weight: 900;text-align: center">Loaded
|
||||
</div>-->
|
||||
</script>
|
||||
<script>
|
||||
var openport ='<^=port^>';
|
||||
console.log('Using port:',openport);
|
||||
</script>
|
||||
|
||||
<script type="text/template" id="main-template">
|
||||
<div class="mui-select mui--bg-primary-dark mui--text-accent">
|
||||
<select id="device" name="device">
|
||||
|
@ -86,12 +86,16 @@ var doInsertEntry = (obj) => {
|
||||
});
|
||||
|
||||
};
|
||||
var lastReceived;
|
||||
var watchDog = 0;
|
||||
var wdTimedOut = false;
|
||||
var watchDogTimeout = () => {
|
||||
'use strict';
|
||||
var now = new Date();
|
||||
logger.warn(
|
||||
'Watchdog timeout. Message has not been received for over 90 seconds.');
|
||||
emailer.sendEmailV1('Watchdog timeout. Message has not been received for over 90 seconds.');
|
||||
emailer.sendEmailV1('Watchdog timeout. Message has not been received for over 90 seconds. \n\nLast received:' + lastReceived + 'Now:' + now);
|
||||
wdTimedOut = true;
|
||||
};
|
||||
|
||||
var mqttClient = function() {
|
||||
@ -172,7 +176,12 @@ var mqttClient = function() {
|
||||
|
||||
busEmitter.emit('saveData', json);
|
||||
clearTimeout(watchDog);
|
||||
watchDog = setTimeout(watchDogTimeout, 20000);
|
||||
watchDog = setTimeout(watchDogTimeout, 90000);
|
||||
lastReceived = new Date();
|
||||
if (wdTimedOut) {
|
||||
emailer.sendEmailV1('Receiving again: ' + message.toString());
|
||||
wdTimedOut = false;
|
||||
}
|
||||
count++;
|
||||
|
||||
}.bind(this));
|
||||
|
Loading…
Reference in New Issue
Block a user