Skip to content

Instantly share code, notes, and snippets.

@luisarmandom
Created September 19, 2012 23:10
Show Gist options
  • Save luisarmandom/3752948 to your computer and use it in GitHub Desktop.
Save luisarmandom/3752948 to your computer and use it in GitHub Desktop.
Most current version
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>
@cronopio
Copy link

Actually there is others problems. You attach the socket.io server to the http server, change that and attach it to app

@cronopio
Copy link

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