--- a/backend/ws/mobile.js
+++ b/backend/ws/mobile.js
@@ -1,21 +1,22 @@
'use strict';
-var initialized = false;
-var NAMESPACE = '/mobileApp';
-var mobileNamespace;
-var user = require('../webserver/api/c2net/db/user.js');
-var usersList = [];
-var activeUser = [];
-
+var initialized = false,
+ NAMESPACE = '/mobileApp',
+ mobileNamespace,
+ user = require('../webserver/api/c2net/db/user.js'),
+ usersList = [],
+ activeUser= [];
+
module.exports = {
- emit: function (id, notif) {
+ emit: function (id,notif){
mobileNamespace.to(id).emit('notification', notif);
}
-};
+}
-function init (dependencies) {
- var logger = dependencies('logger');
- var io = dependencies('wsserver').io;
+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');
@@ -24,66 +25,67 @@
mobileNamespace = io.of(NAMESPACE);
- mobileNamespace.on('connection', function (socket) {
+ mobileNamespace.on('connection', function(socket) {
console.log('New connection on ' + NAMESPACE);
- console.log('############################# SOCKET INFO ##################################');
+
+ 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 (socketInfo.user);
+ var initialNotifs= user.defaultNotication(socketInfo.user, "inital", function(initialNotifications){
console.log(initialNotifications);
- mobileNamespace.to(socketInfo.user).emit('notifications', 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);
+ 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');
+ 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) {
+ var stats= user.getStatistics(socketInfo.user, function(statistics){
console.log(statistics);
- mobileNamespace.to(socketInfo.user).emit('Statistics', 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) {
+ socket.on('subscribe', function(userId) {
console.log('Joining contact room', userId);
socket.join(userId);
- if (usersList.indexOf(userId) !== -1) {
+ 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 () {
+ socket.on('unsubscribe', function() {
console.log('Leaving contact room', socketInfo.user);
socket.leave(socketInfo.user);
var index = activeUser.indexOf(socketInfo.user);
- if (index !== -1) {
+ if (index != -1){
activeUser.splice(index, 1);
console.log('Unsubscribed from a new user:');
console.log(usersList);
@@ -92,6 +94,7 @@
});
});
+
initialized = true;
}