Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var http = require('http'),
- url = require('url'),
- fs = require('fs'),
- io = require('../'),
- sys = require('sys'),
- counter =0,
- path = '',
- server = http.createServer(function(req, res){
- // your normal server code
- path = url.parse(req.url).pathname;
- switch (path){
- case '/':
- res.writeHead(200, {'Content-Type': 'text/html'});
- res.write('<h1>Welcome. Try the <a href="/chat.html">chat</a> example.</h1>');
- res.end();
- break;
- case '/pixel.html':
- case '/json.js':
- case '/report.html':
- fs.readFile(__dirname + path, function(err, data){
- if (err) return send404(res);
- res.writeHead(200, {'Content-Type': path == 'json.js' ? 'text/javascript' : 'text/html'})
- res.write(data, 'utf8');
- res.end();
- });
- break;
- default: send404(res);
- }
- }),
- send404 = function(res){
- res.writeHead(404);
- res.write('404');
- res.end();
- };
- server.listen(80);
- buffer = [];
- var counter = 0;
- // socket.io, I choose you
- // simplest chat application evar
- server.addListener("request", function(socket){
- if (path == '/pixel.html') {
- counter++;
- //io.broadcast({counter});
- console.log('pixel requested! request #' + counter);
- }
- });
- var io = io.listen(server);
- // lost to how i should use the socket.io to pass the counter at an interval of time.
- // say send the counter once a second to any listening clients
- io.on('connection', function(client){
- console.log('someone connected!');
- client.send({ buffer: buffer });
- client.broadcast({ announcement: counter });
- client.on('message', function(message){
- var msg = { message: [client.sessionId, message + ' :: ' + counter] };
- buffer.push(msg);
- if (buffer.length > 15) buffer.shift();
- client.broadcast(msg);
- });
- client.on('disconnect', function(){
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement