blob: e04e13a26ffe8fd147335d27e096171f01c6ff27 [file] [log] [blame]
Matteo Scandolobd13aab2017-08-08 13:05:24 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080017(function () {
18 'use strict';
19
20 const clients = require('./clients.js');
21 const logger = require('../config/logger.js');
22
23 let io;
24
25 exports.create = function(server) {
26 // INSTANTIATE SOCKET.IO
27 // =============================================================================
28
29 io = require('socket.io').listen(server);
30
31 // LISTEN TO "CONNECTION" EVENT (FROM SOCKET.IO)
32 // =============================================================================
33
34 io.on('connection', function (socket) {
35 logger.log('debug', 'connect %j', socket.handshake.query);
36 clients.add(socket.handshake.query);
37
38 socket.emit('connected', {message : 'Welcome to XOS'});
39
40 socket.on('disconnect', function(reason) {
41 clients.remove(socket.handshake.query);
42 logger.log('debug', 'disconnect %s %j', reason, socket.handshake.query);
43 });
44 });
45
46 };
47
48 exports.get = () => io;
49
50 // USAGE
51 // const socketIo = require('./controllers/websocket.js');
52 // const socket = socketIo.get();
53 // socket.emit('eventName', data);
54
Zack Williamsea89ebb2018-09-07 12:37:36 -070055})();