Created
July 24, 2011 09:49
-
-
Save 3rd-Eden/1102456 to your computer and use it in GitHub Desktop.
Socket.IO resource
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
/** | |
* Bootstrap app. | |
*/ | |
require.paths.unshift(__dirname + '/../../lib/'); | |
/** | |
* Module dependencies. | |
*/ | |
var express = require('express') | |
, stylus = require('stylus') | |
, nib = require('nib') | |
, sio = require('socket.io'); | |
/** | |
* App. | |
*/ | |
var app = express.createServer(); | |
/** | |
* App configuration. | |
*/ | |
app.configure(function () { | |
app.use(stylus.middleware({ src: __dirname + '/public', compile: compile })) | |
app.use(express.static(__dirname + '/public')); | |
app.set('views', __dirname); | |
app.set('view engine', 'jade'); | |
function compile (str, path) { | |
return stylus(str) | |
.set('filename', path) | |
.use(nib()); | |
}; | |
}); | |
/** | |
* App routes. | |
*/ | |
app.get('/', function (req, res) { | |
res.render('index', { layout: false }); | |
}); | |
/** | |
* App listen. | |
*/ | |
app.listen(3000, function () { | |
var addr = app.address(); | |
console.log(' app listening on http://' + addr.address + ':' + addr.port); | |
}); | |
/** | |
* Socket.IO server (single process only) | |
*/ | |
var io = sio.listen(app) | |
, nicknames = {}; | |
io.set('resource', '/pewpew') | |
io.sockets.on('connection', function (socket) { | |
socket.on('user message', function (msg) { | |
socket.broadcast.emit('user message', socket.nickname, msg); | |
}); | |
socket.on('nickname', function (nick, fn) { | |
if (nicknames[nick]) { | |
fn(true); | |
} else { | |
fn(false); | |
nicknames[nick] = socket.nickname = nick; | |
socket.broadcast.emit('announcement', nick + ' connected'); | |
io.sockets.emit('nicknames', nicknames); | |
} | |
}); | |
socket.on('disconnect', function () { | |
if (!socket.nickname) return; | |
delete nicknames[socket.nickname]; | |
socket.broadcast.emit('announcement', socket.nickname + ' disconnected'); | |
socket.broadcast.emit('nicknames', nicknames); | |
}); | |
}); |
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
doctype 5 | |
html | |
head | |
link(href='/stylesheets/style.css', rel='stylesheet') | |
script(src='http://code.jquery.com/jquery-1.6.1.min.js') | |
script(src='/pewpew/socket.io.js') | |
script | |
// socket.io specific code | |
var socket = io.connect(null, {resource:'pewpew'}); | |
socket.on('connect', function () { | |
$('#chat').addClass('connected'); | |
}); | |
socket.on('announcement', function (msg) { | |
$('#lines').append($('<p>').append($('<em>').text(msg))); | |
}); | |
socket.on('nicknames', function (nicknames) { | |
$('#nicknames').empty().append($('<span>Online: </span>')); | |
for (var i in nicknames) { | |
$('#nicknames').append($('<b>').text(nicknames[i])); | |
} | |
}); | |
socket.on('user message', message); | |
socket.on('reconnect', function () { | |
$('#lines').remove(); | |
message('System', 'Reconnected to the server'); | |
}); | |
socket.on('reconnecting', function () { | |
message('System', 'Attempting to re-connect to the server'); | |
}); | |
socket.on('error', function (e) { | |
message('System', e ? e : 'A unknown error occurred'); | |
}); | |
function message (from, msg) { | |
$('#lines').append($('<p>').append($('<b>').text(from), msg)); | |
} | |
// dom manipulation | |
$(function () { | |
$('#set-nickname').submit(function (ev) { | |
socket.emit('nickname', $('#nick').val(), function (set) { | |
if (!set) { | |
clear(); | |
return $('#chat').addClass('nickname-set'); | |
} | |
$('#nickname-err').css('visibility', 'visible'); | |
}); | |
return false; | |
}); | |
$('#send-message').submit(function () { | |
message('me', $('#message').val()); | |
socket.emit('user message', $('#message').val()); | |
clear(); | |
$('#lines').get(0).scrollTop = 10000000; | |
return false; | |
}); | |
function clear () { | |
$('#message').val('').focus(); | |
}; | |
}); | |
body | |
#chat | |
#nickname | |
form.wrap#set-nickname | |
p Please type in your nickname and press enter. | |
input#nick | |
p#nickname-err Nickname already in use | |
#connecting | |
.wrap Connecting to socket.io server | |
#messages | |
#nicknames | |
#lines | |
form#send-message | |
input#message | |
button Send |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment