Parent: [65557d] (diff)

Download this file

mobile.js    102 lines (82 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
'use strict';
var initialized = false,
NAMESPACE = '/mobileApp',
mobileNamespace,
user = require('../webserver/api/c2net/db/user.js'),
usersList = [],
activeUser= [];
module.exports = {
emit: function (id,notif){
mobileNamespace.to(id).emit('notification', notif);
}
}
function init(dependencies) {
var logger = dependencies('logger'),
pubsub = dependencies('pubsub').global,
io = dependencies('wsserver').io;
if (initialized) {
logger.warn('The contact notification service is already initialized');
return;
}
mobileNamespace = io.of(NAMESPACE);
mobileNamespace.on('connection', function(socket) {
console.log('New connection on ' + NAMESPACE);
console.log("############################# SOCKET INFO ##################################");
var socketInfo = socket.handshake.query;
user.updateStatus(socketInfo.user, socketInfo.deviceID, 'connect');
user.createUser(socketInfo);
socket.on('initalNotifications', function (data) {
console.log (socketInfo.user);
var initialNotifs= user.defaultNotication(socketInfo.user, "inital", function(initialNotifications){
console.log(initialNotifications);
mobileNamespace.to(socketInfo.user).emit('notifications',initialNotifications);
});
});
socket.on('moreNotifs', function (data) {
console.log (socketInfo.user);
var moreNotifs= user.defaultNotication(socketInfo.user, data, function(moreNotifications){
mobileNamespace.to(socketInfo.user).emit('notifications',moreNotifications);
});
});
socket.on('readNotification', function (notifsId) {
console.log (socketInfo.user);
var moreNotifs= user.readNotification(socketInfo.user, notifsId);
mobileNamespace.to(socketInfo.user).emit('updateNotification',"try to update");
});
socket.on('getStatistics', function (data) {
console.log(data);
var stats= user.getStatistics(socketInfo.user, function(statistics){
console.log(statistics);
mobileNamespace.to(socketInfo.user).emit('Statistics',statistics);
});
});
socket.on('disconnect', function () {
user.updateStatus(socketInfo.user, socketInfo.deviceID, 'disconnect');
console.log('User disconnected:');
});
socket.on('subscribe', function(userId) {
console.log('Joining contact room', userId);
socket.join(userId);
if (usersList.indexOf(userId) != -1){
usersList.push(userId);
activeUser.push(userId);
console.log('Subscribed from a new user:');
console.log(usersList);
console.log(activeUser);
};
});
socket.on('unsubscribe', function() {
console.log('Leaving contact room', socketInfo.user);
socket.leave(socketInfo.user);
var index = activeUser.indexOf(socketInfo.user);
if (index != -1){
activeUser.splice(index, 1);
console.log('Unsubscribed from a new user:');
console.log(usersList);
console.log(activeUser);
}
});
});
initialized = true;
}
module.exports.init = init;