Created
March 24, 2013 20:22
-
-
Save zivester/5233347 to your computer and use it in GitHub Desktop.
socket.io single client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var express = require('express'), | |
| http = require('http'), | |
| _ = require('underscore'), | |
| app = express(), | |
| server = http.createServer(app), | |
| io = require('socket.io').listen(server); | |
| // Sockets | |
| var pool = {}; | |
| setInterval(function(){ | |
| var total = _.size(pool), | |
| active = 0; | |
| _.each(pool, function(p, id){ | |
| if (!p.socket){ | |
| console.log("socket doesn't exist, removing"); | |
| delete pool[id]; | |
| return; | |
| } | |
| active++; | |
| p.socket.broadcast.emit('hello', { | |
| clients : total, | |
| date_updated : new Date() | |
| }); | |
| }); | |
| console.log("emitted to " + active); | |
| }, 1000); | |
| // Socket.io | |
| io.sockets.on('connection', function (socket) { | |
| var id = socket.id; | |
| pool[id] = { | |
| socket : socket | |
| }; | |
| socket.on('disconnect', function(socket){ | |
| console.log("DISCONNECT", id); | |
| delete pool[id]; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment