Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // note, io.listen() will create a http server for you
- var io = require('socket.io').listen(1334);
- var mysql = require('mysql');
- var sys = require('sys');
- var net = require('net');
- var server = net.createServer(function (stream) {
- stream.setEncoding("utf8");
- stream.on("connect", function () {
- // I could reject the connection if I wanted to here. (I think??);
- });
- stream.on('data', function (data) {
- message = JSON.parse(data);
- console.log(message);
- // Now I want to check if the user is connected, if they are, then I want to send them a message.
- console.log( users );
- console.log( ">>>>>>> Checking if is Connected", message.id );
- if( users.isConnected( message.id ) ) {
- console.log( "User is Connected");
- user = users.getUser( message.id);
- user.incrementNotificationCount();
- user.sendNotificationsCount();
- }
- });
- });
- server.listen(8124, 'localhost');
- // on the server
- var user = function(){ };
- user.prototype = {
- sockets: [],
- user : "",
- notificationCount : false,
- hasNotificationCount : function(){
- return !!this.notificationCount;
- },
- setNotificationCount: function( count ){
- this.notificationCount = count;
- },
- getNotificationCount: function( ){
- return this.notificationCount;
- },
- incrementNotificationCount: function(){
- this.notificationCount = this.notificationCount + 1;
- },
- sendNotificationsCount: function(){
- count = this.getNotificationCount( );
- for( socket in this.sockets) {
- console.log("Trying to emit for socket ID", socket);
- io.sockets.emit('notification count', { count : count });
- io.sockets.socket( socket ).emit('notification count', { count : count });
- }
- }
- };
- var users = {
- tokens : {},
- getUser: function(key){
- return this.tokens[ key];
- },
- setUser: function(){
- },
- checkinUser: function(incoming, socketId){
- // tokens = User IDs or whatever you use to identify the user
- this.tokens[ incoming.token ] = this.tokens[incoming.token] || new user();
- this.tokens[ incoming.token ].sockets.push( socketId );
- this.tokens[ incoming.token ].user = incoming.user;
- return incoming.token;
- },
- isConnected: function(key){
- console.log( ">>>>>>>>>>>>>>" , this.tokens[key]);
- return !!this.tokens[key];
- }
- };
- var client = mysql.createClient({
- host: 'localhost',
- user: 'root',
- password: '',
- port: '3306',
- database: 'discovery'
- });
- io.configure(function (){
- io.set('authorization', function (handshakeData, callback) {
- callback(null, true); // error first callback style
- });
- });
- io.sockets.on('connection', function (socket) {
- console.log(users);
- socket.emit('who are you');
- socket.on('new notification', function(notification){
- });
- socket.on('check in', function(incoming){
- userToken = users.checkinUser(incoming, socket.id);
- console.log("Adding a new user checkin", incoming.token);
- // Get the Count of unread messages if the user does not already have been loading.
- if ( !users.getUser(userToken).hasNotificationCount() ){
- client.query('SELECT count(id) as count from messages where sentTo = ? and hasRead = 0', [ incoming.token ], function(err,results,fields){
- users.getUser(userToken).setNotificationCount( results[0].count );
- socket.emit('notification count', { count : users.getUser(userToken).getNotificationCount( ) });
- });
- } else {
- socket.emit('notification count', { count : users.getUser(userToken).getNotificationCount( ) });
- }
- });
- socket.on('disconnect', function () {
- console.log(socket.id, " => disconnected");
- io.sockets.emit('user disconnected');
- });
- });
Add Comment
Please, Sign In to add comment