Advertisement
Guest User

Untitled

a guest
Jan 5th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. io            = require 'socket.io'
  2. jwt           = require 'jsonwebtoken'
  3. clientManager = require 'client-manager'
  4. SocketManager = require './SocketManager'
  5.  
  6. module.exports = class Sockets
  7.     constructor: (@app, @server) ->
  8.         @loaded            = false
  9.         @app.io            = io @server
  10.         @manager           = clientManager()
  11.         @socketManager     = new SocketManager @manager
  12.         @app.socketManager = @socketManager
  13.  
  14.         @listenEvents()
  15.         @app.logger.info '✓ '.bold.green + 'Started sockets handler.'
  16.  
  17.     listenEvents: () ->
  18.         usersController = require('../../api/users/index')(@app, @app.Sequelize, @app.db);
  19.         @app.sockets_channels = {}
  20.  
  21.         @app.joinChannel = (name, userId) =>
  22.             if typeof @app.sockets_channels[name] == 'undefined'
  23.                 @app.sockets_channels[name] = []
  24.  
  25.             @app.sockets_channels[name].push userId
  26.             sock = @app.socketManager.getClientSocket(userId)
  27.             sock.join name if sock
  28.  
  29.         @app.unsubscribeChannel = (channel, socket) =>
  30.             if typeof socket.user != 'undefined'
  31.                 for own key, value of @app.sockets_channels
  32.                     if key.match(new RegExp "#{channel}_") and Array.isArray value
  33.                         @app.sockets_channels[key] = value.filter (u) => u != socket.user.id
  34.                         socket.leave key
  35.  
  36.                         delete @app.sockets_channels[key] if @app.sockets_channels[key].length == 0
  37.  
  38.         @app.unsubscribeAllChannels = (socket) =>
  39.             if typeof socket.user != 'undefined'
  40.                 for own key, value of @app.sockets_channels
  41.                     if Array.isArray value
  42.                         @app.sockets_channels[key] = value.filter (u) => u != socket.user.id
  43.                         socket.leave key
  44.                         delete @app.sockets_channels[key] if @app.sockets_channels[key].length == 0
  45.  
  46.         @app.io.on 'connection', (socket) =>
  47.  
  48.             socket.on 'join', (data) =>
  49.                 jwt.verify data.authorization, @app.config.jwt_secret, (err, user) =>
  50.                     if err is null
  51.                         socket.user = user;
  52.                         @manager.addClient user.id, socket
  53.  
  54.                         usersController.getFriends(user.id).then (friends) =>
  55.                             user.status = 'online'
  56.                             friends.forEach (friend) =>
  57.                                 c = @socketManager.getClientSocket friend.friend_id
  58.                                 if c
  59.                                     @app.io.to(c.id).emit 'onUserStatus', user
  60.                         .catch (error) =>
  61.                             console.log error
  62.  
  63.             socket.on 'unsubscribe_channel', (channel) =>
  64.                 @app.unsubscribeChannel channel, socket
  65.  
  66.             socket.on 'disconnect', =>
  67.                 client = @socketManager.getClientBySocketId socket.id
  68.                 if client isnt null
  69.                     client.status = 'offline'
  70.                     @app.unsubscribeAllChannels(socket);
  71.                     usersController.getFriends(client.id).then (friends) =>
  72.                         friends.forEach (friend) =>
  73.                             c = @socketManager.getClientSocket friend.friend_id
  74.                             if c
  75.                                 @app.io.to(c.id).emit 'onUserStatus', client
  76.  
  77.                     @manager.removeClient client.id
  78.                     @app.logger.info "#{client.firstname} #{client.lastname} has disconnected"
  79.  
  80.         @manager.on 'add', (client) =>
  81.             @app.logger.info "#{client.user.firstname} #{client.user.lastname} is connected"
  82.  
  83.         @manager.on 'empty', =>
  84.             @app.logger.info 'No more sockets connected'
  85.  
  86.         @loaded = true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement