Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   io.sockets.on('connection', function (socket) {
  2.         var origin = socket.request.headers['origin'];
  3.         console.log('A client has connected to from: ' + origin);
  4.         io.emit('demo-2', 'Authorizing your connection..');
  5.  
  6.         pool.getConnection(function(err, connection) {
  7.  
  8.           //get connection origin
  9.           var key = socket.request.headers['origin'];
  10.           var getAppInformation = 'SELECT * FROM users_app WHERE origin = ?';
  11.  
  12.           connection.query(getAppInformation, [key], function(err, result, fields) {
  13.               if (err) throw err
  14.  
  15.               if(result.length < 1){
  16.                 io.emit('demo-2', 'You are not authorized on this network. Disconnecting.')
  17.                 return socket.disconnect()
  18.               }
  19.  
  20.               //Client requesting to join a channel
  21.               socket.on('join', function (data) {
  22.                 console.log(data);
  23.  
  24.                 if(!data['channel']){
  25.                   //error just return nothing and stop
  26.                   return
  27.                 }
  28.  
  29.                 //Client requesting to join public channel
  30.                 if(!data['presense'] || data['presense'] === false){
  31.                   publicChannel = data['channel']
  32.                   socket.join(publicChannel)
  33.                 }
  34.  
  35.                 //Client requesting to join Presence channel
  36.                 if(data['presence'] === true && data['authorize'] === false ||
  37.                    data['presence'] === true && !data['authorize']){
  38.                   presenceChannel = data['channel']
  39.                   socket.join(presenceChannel)
  40.  
  41.                   //trigger callback to origin/pushthis/auth
  42.                       callback = {
  43.                         'status'  : 'connected',
  44.                         'channel' : data['channel'],
  45.                         'origin'  : socket.request.headers['origin'],
  46.                         'socket_id' : socket.id
  47.                       };
  48.  
  49.  
  50.                       //send back to the customers site
  51.                       io.to(presenceChannel).emit('info', callback);
  52.                 }
  53.  
  54.                 if(data['authorize'] === true){
  55.                   presenceChannel = data['channel']
  56.                   //trigger callback to origin/pushthis/auth
  57.                       callback = {
  58.                         'status'  : 'waiting authorization',
  59.                         'channel' : data['channel'],
  60.                         'origin'  : socket.request.headers['origin'],
  61.                         'socket_id' : socket.id
  62.                       };
  63.  
  64.  
  65.                       //send back to the customers site
  66.                       io.to(presenceChannel).emit('info', callback);
  67.                 }
  68.               });
  69.               var status     = "connection";
  70.               var identifier = result[0]['users_app_id'];
  71.               //var identifier = result['users_app_id'];
  72.               track.customersConnections(identifier, status, pool);
  73.           });
  74.         // And done with the connection.
  75.         connection.release();
  76.         });
  77.  
  78.  
  79.       socket.once('disconnect', function (reason, socketID, channels) {
  80.         //let the customer know someone left their channels
  81.             for(var channel in channels) {
  82.                 if(channel === socketID){
  83.                   //ignore
  84.                 }else{
  85.                   //trigger callback to customer
  86.                       callback = {
  87.                         'status'  : 'disconnected',
  88.                         'channel' : channel,
  89.                         'origin'  : socket.request.headers['origin'],
  90.                         'socket_id' : socketID
  91.                       };
  92.  
  93.                       io.to(channel).emit('info', callback);
  94.                 }
  95.             }
  96.  
  97.         console.log('A client has disconnected from from: ' + socket.request.headers['origin']);
  98.  
  99.         pool.getConnection(function(err, connection) {
  100.           var status = "disconnection";
  101.           var key = socket.request.headers['origin'];
  102.           var getAppInformation = 'SELECT * FROM users_app WHERE origin = ?';
  103.  
  104.           connection.query(getAppInformation, [key], function(err, result, fields) {
  105.               if (err) throw error
  106.               if(result.length < 1){
  107.                 return
  108.               }
  109.  
  110.  
  111.               var status     = "disconnection";
  112.               var identifier = result[0]['users_app_id'];
  113.  
  114.               track.customersConnections(identifier, status, pool);
  115.  
  116.           });
  117.         // And done with the connection.
  118.         connection.release();
  119.         });
  120.       });
  121.   });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement