blob: e048419214f726d9d361c148b3a6d599594ccb42 [file] [log] [blame]
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08001(function () {
2 'use strict';
3
4 const clients = require('./clients.js');
5 const logger = require('../config/logger.js');
6
7 let io;
8
9 exports.create = function(server) {
10 // INSTANTIATE SOCKET.IO
11 // =============================================================================
12
13 io = require('socket.io').listen(server);
14
15 // LISTEN TO "CONNECTION" EVENT (FROM SOCKET.IO)
16 // =============================================================================
17
18 io.on('connection', function (socket) {
19 logger.log('debug', 'connect %j', socket.handshake.query);
20 clients.add(socket.handshake.query);
21
22 socket.emit('connected', {message : 'Welcome to XOS'});
23
24 socket.on('disconnect', function(reason) {
25 clients.remove(socket.handshake.query);
26 logger.log('debug', 'disconnect %s %j', reason, socket.handshake.query);
27 });
28 });
29
30 };
31
32 exports.get = () => io;
33
34 // USAGE
35 // const socketIo = require('./controllers/websocket.js');
36 // const socket = socketIo.get();
37 // socket.emit('eventName', data);
38
39})();