Skip to content

Instantly share code, notes, and snippets.

@yoni
Created August 27, 2010 02:31
Show Gist options
  • Save yoni/552670 to your computer and use it in GitHub Desktop.
Save yoni/552670 to your computer and use it in GitHub Desktop.
var http = require('http'),
express = require('express'),
connect = require('connect'),
url = require('url'),
fs = require('fs'),
io = require('../'),
sys = require('sys');
var app = require('express').createServer();
app.use('/', connect.staticProvider(__dirname + '/'));
app.get('/', function(req, res){
res.send('<h1>Welcome. Try the <a href="/chat.html">chat</a> example.</h1>');
});
app.listen(3000);
// socket.io, I choose you
// simplest chat application evar
var io = io.listen(app),
buffer = [];
io.on('connection', function(client){
client.send({ buffer: buffer });
client.broadcast({ announcement: client.sessionId + ' connected' });
client.on('message', function(message){
var msg = { message: [client.sessionId, message] };
buffer.push(msg);
if (buffer.length > 15) buffer.shift();
client.broadcast(msg);
});
client.on('disconnect', function(){
client.broadcast({ announcement: client.sessionId + ' disconnected' });
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment