blob: 30a78855c89a78a426f1ebd2c4af332a21c96df6 [file] [log] [blame]
/*
* Copyright 2017-present Open Networking Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
(function () {
'use strict';
const clients = require('./clients.js');
const logger = require('../config/logger.js');
let io;
exports.create = function(server) {
// INSTANTIATE SOCKET.IO
// =============================================================================
io = require('socket.io').listen(server);
// LISTEN TO "CONNECTION" EVENT (FROM SOCKET.IO)
// =============================================================================
io.on('connection', function (socket) {
logger.log('debug', 'connect %j', socket.handshake.query);
clients.add(socket.handshake.query);
socket.emit('connected', {message : 'Welcome to XOS'});
socket.on('disconnect', function(reason) {
clients.remove(socket.handshake.query);
logger.log('debug', 'disconnect %s %j', reason, socket.handshake.query);
});
});
};
exports.get = () => io;
// USAGE
// const socketIo = require('./controllers/websocket.js');
// const socket = socketIo.get();
// socket.emit('eventName', data);
})();