Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- io.sockets.on('connection', function (socket) {
- var origin = socket.request.headers['origin'];
- console.log('A client has connected to from: ' + origin);
- io.emit('demo-2', 'Authorizing your connection..');
- pool.getConnection(function(err, connection) {
- //get connection origin
- var key = socket.request.headers['origin'];
- var getAppInformation = 'SELECT * FROM users_app WHERE origin = ?';
- connection.query(getAppInformation, [key], function(err, result, fields) {
- if (err) throw err
- if(result.length < 1){
- io.emit('demo-2', 'You are not authorized on this network. Disconnecting.')
- return socket.disconnect()
- }
- //Client requesting to join a channel
- socket.on('join', function (data) {
- console.log(data);
- if(!data['channel']){
- //error just return nothing and stop
- return
- }
- //Client requesting to join public channel
- if(!data['presense'] || data['presense'] === false){
- publicChannel = data['channel']
- socket.join(publicChannel)
- }
- //Client requesting to join Presence channel
- if(data['presence'] === true && data['authorize'] === false ||
- data['presence'] === true && !data['authorize']){
- presenceChannel = data['channel']
- socket.join(presenceChannel)
- //trigger callback to origin/pushthis/auth
- callback = {
- 'status' : 'connected',
- 'channel' : data['channel'],
- 'origin' : socket.request.headers['origin'],
- 'socket_id' : socket.id
- };
- //send back to the customers site
- io.to(presenceChannel).emit('info', callback);
- }
- if(data['authorize'] === true){
- presenceChannel = data['channel']
- //trigger callback to origin/pushthis/auth
- callback = {
- 'status' : 'waiting authorization',
- 'channel' : data['channel'],
- 'origin' : socket.request.headers['origin'],
- 'socket_id' : socket.id
- };
- //send back to the customers site
- io.to(presenceChannel).emit('info', callback);
- }
- });
- var status = "connection";
- var identifier = result[0]['users_app_id'];
- //var identifier = result['users_app_id'];
- track.customersConnections(identifier, status, pool);
- });
- // And done with the connection.
- connection.release();
- });
- socket.once('disconnect', function (reason, socketID, channels) {
- //let the customer know someone left their channels
- for(var channel in channels) {
- if(channel === socketID){
- //ignore
- }else{
- //trigger callback to customer
- callback = {
- 'status' : 'disconnected',
- 'channel' : channel,
- 'origin' : socket.request.headers['origin'],
- 'socket_id' : socketID
- };
- io.to(channel).emit('info', callback);
- }
- }
- console.log('A client has disconnected from from: ' + socket.request.headers['origin']);
- pool.getConnection(function(err, connection) {
- var status = "disconnection";
- var key = socket.request.headers['origin'];
- var getAppInformation = 'SELECT * FROM users_app WHERE origin = ?';
- connection.query(getAppInformation, [key], function(err, result, fields) {
- if (err) throw error
- if(result.length < 1){
- return
- }
- var status = "disconnection";
- var identifier = result[0]['users_app_id'];
- track.customersConnections(identifier, status, pool);
- });
- // And done with the connection.
- connection.release();
- });
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement