Guest User

Untitled

a guest
Dec 12th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. // note, io.listen() will create a http server for you
  2. var io = require('socket.io').listen(1334);
  3. var mysql = require('mysql');
  4. var sys = require('sys');
  5. var net = require('net');
  6. var server = net.createServer(function (stream) {
  7. stream.setEncoding("utf8");
  8.  
  9. stream.on("connect", function () {
  10. // I could reject the connection if I wanted to here. (I think??);
  11. });
  12.  
  13. stream.on('data', function (data) {
  14. message = JSON.parse(data);
  15. console.log(message);
  16. // Now I want to check if the user is connected, if they are, then I want to send them a message.
  17. console.log( users );
  18. console.log( ">>>>>>> Checking if is Connected", message.id );
  19. if( users.isConnected( message.id ) ) {
  20. console.log( "User is Connected");
  21. user = users.getUser( message.id);
  22. user.incrementNotificationCount();
  23. user.sendNotificationsCount();
  24. }
  25. });
  26.  
  27. });
  28. server.listen(8124, 'localhost');
  29.  
  30. // on the server
  31. var user = function(){ };
  32. user.prototype = {
  33. sockets: [],
  34. user : "",
  35. notificationCount : false,
  36. hasNotificationCount : function(){
  37. return !!this.notificationCount;
  38. },
  39. setNotificationCount: function( count ){
  40. this.notificationCount = count;
  41. },
  42. getNotificationCount: function( ){
  43. return this.notificationCount;
  44. },
  45. incrementNotificationCount: function(){
  46. this.notificationCount = this.notificationCount + 1;
  47. },
  48. sendNotificationsCount: function(){
  49. count = this.getNotificationCount( );
  50. for( socket in this.sockets) {
  51. console.log("Trying to emit for socket ID", socket);
  52. io.sockets.emit('notification count', { count : count });
  53. io.sockets.socket( socket ).emit('notification count', { count : count });
  54. }
  55. }
  56.  
  57. };
  58. var users = {
  59. tokens : {},
  60. getUser: function(key){
  61. return this.tokens[ key];
  62. },
  63. setUser: function(){
  64.  
  65. },
  66. checkinUser: function(incoming, socketId){
  67. // tokens = User IDs or whatever you use to identify the user
  68. this.tokens[ incoming.token ] = this.tokens[incoming.token] || new user();
  69. this.tokens[ incoming.token ].sockets.push( socketId );
  70. this.tokens[ incoming.token ].user = incoming.user;
  71. return incoming.token;
  72. },
  73. isConnected: function(key){
  74. console.log( ">>>>>>>>>>>>>>" , this.tokens[key]);
  75. return !!this.tokens[key];
  76. }
  77.  
  78. };
  79.  
  80.  
  81.  
  82. var client = mysql.createClient({
  83. host: 'localhost',
  84. user: 'root',
  85. password: '',
  86. port: '3306',
  87. database: 'discovery'
  88. });
  89.  
  90. io.configure(function (){
  91. io.set('authorization', function (handshakeData, callback) {
  92. callback(null, true); // error first callback style
  93. });
  94. });
  95.  
  96.  
  97. io.sockets.on('connection', function (socket) {
  98. console.log(users);
  99. socket.emit('who are you');
  100.  
  101. socket.on('new notification', function(notification){
  102.  
  103.  
  104. });
  105. socket.on('check in', function(incoming){
  106. userToken = users.checkinUser(incoming, socket.id);
  107. console.log("Adding a new user checkin", incoming.token);
  108.  
  109. // Get the Count of unread messages if the user does not already have been loading.
  110. if ( !users.getUser(userToken).hasNotificationCount() ){
  111. client.query('SELECT count(id) as count from messages where sentTo = ? and hasRead = 0', [ incoming.token ], function(err,results,fields){
  112. users.getUser(userToken).setNotificationCount( results[0].count );
  113. socket.emit('notification count', { count : users.getUser(userToken).getNotificationCount( ) });
  114. });
  115. } else {
  116. socket.emit('notification count', { count : users.getUser(userToken).getNotificationCount( ) });
  117. }
  118. });
  119.  
  120. socket.on('disconnect', function () {
  121. console.log(socket.id, " => disconnected");
  122. io.sockets.emit('user disconnected');
  123. });
  124. });
Add Comment
Please, Sign In to add comment