diff --git a/main.js b/main.js index 973e9f4..76cb512 100644 --- a/main.js +++ b/main.js @@ -33,6 +33,15 @@ if (os.platform() === 'linux') { var shepherd = require('./routes/shepherd'), guiapp = express(); +guiapp.use(function(req, res, next) { + res.header("Access-Control-Allow-Origin", "http://127.0.0.1:17777"); + res.header("Access-Control-Allow-Headers", "X-Requested-With"); + res.header("Access-Control-Allow-Credentials", "true"); + res.header("Access-Control-Allow-Headers", "Content-Type"); + res.header("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS"); + next(); + }); + var appConfig = shepherd.loadLocalConfig(); // load app config // preload.js @@ -71,10 +80,37 @@ guiapp.use('/shepherd', shepherd); console.log(cluster) */ -var rungui = guiapp.listen(appConfig.iguanaAppPort, function () { +/*var rungui = guiapp.listen(appConfig.iguanaAppPort, function () { console.log('guiapp listening on port ' + appConfig.iguanaAppPort + '!'); +});*/ + +var server = require('http').createServer(guiapp); +var io = require('socket.io').listen(server); +server.listen(appConfig.iguanaAppPort, function() { + console.log('guiapp and sockets.io are listening on port ' + appConfig.iguanaAppPort + '!'); }); +io.set('origins', 'http://127.0.0.1:17777'); + +io.on('connection', function(client) { + console.log('EDEX GUI is connected...'); + + client.on('event', function(data) { + console.log(data); + }); + client.on('disconnect', function(data) { + console.log('EDEX GUI is disconnected'); + }); + client.on('join', function(data) { + console.log(data); + client.emit('messages', 'Sockets server is listening'); + }); +}); + +shepherd.setIO(io); + +//io.emit('an event sent to all connected clients'); + module.exports = guiapp; // END GUI App Settings diff --git a/package.json b/package.json index 72f95dd..744cc7f 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "mkdirp": "^0.5.1", "pm2": "^2.3.0", "ps-node": "^0.1.4", - "read-last-lines": "^1.1.0" + "read-last-lines": "^1.1.0", + "socket.io": "^1.7.3" } } diff --git a/routes/shepherd.js b/routes/shepherd.js index e47535b..08fe046 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -91,6 +91,16 @@ shepherd.get('/appconf', function(req, res, next) { res.send(obj); }); +shepherd.get('/socket-test', function(req, res, next) { + res.send('Sockets test'); + shepherd.io.emit('messages', { 'message': 'legacy of grewal' }); +}); + +// expose sockets obj +shepherd.setIO = function(io) { + shepherd.io = io; +}; + /* * params: pubkey */