PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/socketI0/socket.js

https://github.com/ajaylbsim/Me-You
JavaScript | 155 lines | 123 code | 23 blank | 9 comment | 16 complexity | 4ee18d4cf63d459fd9792776d2af235b MD5 | raw file
  1. exports.startSocket = function () {
  2. //console.log('------------------------------------------- cool')
  3. var clients = {};
  4. var socketsOfClients = {};
  5. io.sockets.on('connection', function (socket) {
  6. socket.on('set username', function (userId) {
  7. if (clients[userId] === undefined) {
  8. // Does not exist ... so, proceed
  9. clients[userId] = socket.id;
  10. socketsOfClients[socket.id] = userId;
  11. // console.log('total users ================= ', socketsOfClients);
  12. updateFrindsList(userId);
  13. userIdAvailable(socket.id, userId);
  14. userJoined(userId);
  15. broadcastOnline(userId);
  16. } else if (clients[userId] === socket.id) {
  17. // Ignore for now
  18. } else {
  19. userIdAlreadyInUse(socket.id, userId);
  20. }
  21. });
  22. socket.on('message', function (msg) {
  23. console.log('--------------------------------message found ----------- ', msg)
  24. var srcUser;
  25. if (msg.inferSrcUser) {
  26. // Infer user name based on the socket id
  27. srcUser = socketsOfClients[socket.id];
  28. } else {
  29. srcUser = msg.source;
  30. }
  31. if (msg.target == "All") {
  32. // broadcast
  33. io.sockets.emit('message',
  34. {"source": srcUser,
  35. "message": msg.message,
  36. "target": msg.target,
  37. "message": msg.message,
  38. "deliveredTime": msg.deliveredTime});
  39. } else {
  40. // Look up the socket id
  41. if(clients[msg.target])
  42. {
  43. io.sockets.sockets[clients[msg.target]].emit('message',
  44. { "source": srcUser,
  45. "message": msg.message,
  46. "target": msg.target,
  47. "deliveredTime": msg.deliveredTime});
  48. console.log("message is send to ", clients[msg.target])
  49. }else{
  50. console.log("target is ofline so can't send message ", clients[msg.target])
  51. }
  52. var messageObj=new message();
  53. messageObj.To=msg.target;
  54. messageObj.From=srcUser;
  55. messageObj.data=msg.message;
  56. messageObj.deliveredTime=new Date();
  57. messageObj.save(function(err,msgObj){
  58. if(err)console.log(err);
  59. });
  60. }
  61. })
  62. socket.on('disconnect', function () {
  63. var uName = socketsOfClients[socket.id];
  64. console.log('==================*****************************disconnected********',uName)
  65. broadcastOfline(uName)
  66. delete socketsOfClients[socket.id];
  67. delete clients[uName];
  68. // relay this message to all the clients
  69. userLeft(uName);
  70. })
  71. })
  72. function userJoined(uName) {
  73. Object.keys(socketsOfClients).forEach(function (sId) {
  74. io.sockets.sockets[sId].emit('userJoined', { "userId": uName, "userList": socketsOfClients});
  75. })
  76. }
  77. function updateFrindsList(userId) {
  78. Object.keys(socketsOfClients).forEach(function (sId) {
  79. userService.findFriends(userId)
  80. .on('data', function (friends) {
  81. if (friends.length && friends[0].friends.length) {
  82. //initilizig status
  83. friends[0].friends.forEach(function (obj) {
  84. obj.status = false;
  85. });
  86. for (var key in clients) {
  87. friends[0].friends.forEach(function (obj) {
  88. if (key == obj.id) {
  89. log.info(">>>>>>>>>>>>>>>>>>>>>>online is >>>>>>>>>>>>>>", obj.name);
  90. obj.status = true;
  91. }
  92. })
  93. }
  94. }
  95. io.sockets.sockets[sId].emit('updateFrindsList', {"userList": friends[0].friends});
  96. })
  97. .on('err', function () {
  98. log.error('error occured !!!!');
  99. })
  100. })
  101. }
  102. function userLeft(uName) {
  103. io.sockets.emit('userLeft', { "userId": uName });
  104. }
  105. function userIdAvailable(sId, uName) {
  106. setTimeout(function () {
  107. console.log('Sending welcome msg to ' + uName + ' at ' + sId);
  108. io.sockets.sockets[sId].emit('welcome', { "userId": uName, "currentUsers": JSON.stringify(Object.keys(clients)) });
  109. }, 500);
  110. }
  111. function userIdAlreadyInUse(sId, uName) {
  112. setTimeout(function () {
  113. io.sockets.sockets[sId].emit('error', { "userIdInUse": true });
  114. }, 500);
  115. }
  116. function broadcastOnline(sId) {
  117. io.sockets.emit('catchOnlineFriends',
  118. {
  119. "id": sId
  120. });
  121. setTimeout(function () {
  122. }, 500);
  123. }
  124. function broadcastOfline(sId) {
  125. io.sockets.emit('catchOflineFriends',
  126. {
  127. "id": sId
  128. });
  129. setTimeout(function () {
  130. }, 500);
  131. }
  132. }