Created
September 19, 2012 23:10
-
-
Save luisarmandom/3752948 to your computer and use it in GitHub Desktop.
Most current version
This file contains 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
Server: | |
var app = require('express')(), | |
server = require('http').createServer(app), | |
io = require('socket.io').listen(server); | |
server.listen(8088); | |
//routing | |
app.get('/', function (req, res) { | |
res.sendfile(__dirname + '/index.html'); | |
}); | |
io.sockets.on('connection', function (socket) { | |
socket.on('set nickname', function (name) { | |
socket.set('nickname', name, function () { socket.emit('ready'); }); | |
}); | |
socket.on('msg', function () { | |
socket.get('nickname', function (err, name) { | |
console.log('Chat message by ', name); | |
}); | |
}); | |
socket.on('disconnect', function () { | |
io.sockets.emit('user disconnected'); | |
console.log("disconnected"); | |
}); | |
}); | |
Client: | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect();//without a URI it goes to auto-detect | |
socket.on('connect', function () { | |
socket.emit('set nickname', prompt('What is your nickname?')); | |
socket.on('ready', function () { | |
console.log('Connected !'); | |
socket.emit('msg', prompt('What is your message?')); | |
}); | |
}); | |
</script> |
nevermind, express 3 does exactly the opposite of what I'm saying this code should be fine
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually there is others problems. You attach the socket.io server to the http server, change that and attach it to
app