{"size": 1062, "ext": "js", "max_stars_repo_path": "examples/web/lib/sockets.js", "max_stars_repo_name": "jdarling/radxa-rock", "max_stars_repo_stars_event_min_datetime": "2015-02-10T08:54:27.000Z", "max_stars_repo_stars_event_max_datetime": "2020-06-21T09:20:00.000Z", "max_issues_repo_path": "examples/web/lib/sockets.js", "max_issues_repo_name": "jdarling/radxa-rock", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "examples/web/lib/sockets.js", "max_forks_repo_name": "jdarling/radxa-rock", "max_forks_repo_forks_event_min_datetime": "2015-05-22T08:13:12.000Z", "max_forks_repo_forks_event_max_datetime": "2020-06-21T09:20:02.000Z"}
{"max_stars_count": 7, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 24.6976744186, "max_line_length": 64, "alphanum_fraction": 0.6073446328}
var SocketIO = require('socket.io');
var logger = require('../lib/logger');
var config = require('../lib/config').section('socket_io', {});
var io;

var Sockets = function(){
  var self = this;
  self._listeners = [];
};

Sockets.prototype.init = function(server){
  var self = this;
  self.io = SocketIO.listen(server, config);
  self.io.sockets.on('connection', function(socket){
    socket.on('message', function(msg){
      logger.log('Message: ', msg);
      socket.broadcast.emit('message', msg);
    });
    self._listeners.forEach(function(listener){
      socket.on(listener.event, (function(handler){
        return function(data, fn){
          handler(data, socket, fn);
        };
      })(listener.handler));
    });
  });
};

Sockets.prototype.on = function(event, handler){
  var self = this;
  self._listeners.push({
    event: event,
    handler: handler
  });
};

Sockets.prototype.broadcast = function(event, msg){
  var self = this;
  self.io.sockets.emit(event, msg);
};

module.exports = Sockets;
