socket stuff

This commit is contained in:
Martin Donnelly 2016-02-17 11:16:58 +00:00
parent bac6fc5c18
commit 5fff5400fc

View File

@ -65,7 +65,7 @@ function insertTempReading(time, reading) {
temp_db.run('INSERT into temp(date, reading) VALUES (?,?);', [time, reading]); temp_db.run('INSERT into temp(date, reading) VALUES (?,?);', [time, reading]);
temp_db.run('commit'); temp_db.run('commit');
busEmitter.emit("wsSend", reading); busEmitter.emit("sendSocket", reading);
} }
router.get('/', function (req, res, next) { router.get('/', function (req, res, next) {
@ -139,14 +139,17 @@ busEmitter.on('saveTempData', doInsertEvent);
// Socket Stuff // Socket Stuff
logger.info('Configuring WebSocket Listener...'); logger.info('Configuring WebSocket Listener...');
logger.info('Creating server...');
var server = http.createServer(function (request, response) { var server = http.createServer(function (request, response) {
logger.info((new Date()) + ' Received request for ' + request.url); logger.info((new Date()) + ' Received request for ' + request.url);
response.writeHead(404); response.writeHead(404);
response.end(); response.end();
}); });
logger.info('Creating listener...');
server.listen(8031, function () { server.listen(8031, function () {
logger.info((new Date()) + ' Server is listening on port 8031'); logger.info((new Date()) + ' Server is listening on port 8031');
}); });
logger.info('Creating wsServer...');
var wsServer = new WebSocketServer({ var wsServer = new WebSocketServer({
httpServer: server, httpServer: server,
// You should not use autoAcceptConnections for production // You should not use autoAcceptConnections for production
@ -178,7 +181,7 @@ wsServer.on('request', function (request) {
logger.info('sendSocket: ' + JSON.stringify(obj)); logger.info('sendSocket: ' + JSON.stringify(obj));
connection.sendUTF(JSON.stringify(obj)); connection.sendUTF(JSON.stringify(obj));
} }
busEmitter.on('wsSend', sendSocketHandler); busEmitter.on('sendSocket', sendSocketHandler);
connection.on('message', function (message) { connection.on('message', function (message) {
if (message.type === 'utf8') { if (message.type === 'utf8') {
@ -196,4 +199,4 @@ wsServer.on('request', function (request) {
logger.debug((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); logger.debug((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
busEmitter.removeListener('sendSocket', sendSocketHandler); busEmitter.removeListener('sendSocket', sendSocketHandler);
}); });
});;;; });;