Created
November 25, 2011 21:16
-
-
Save cronopio/1394428 to your computer and use it in GitHub Desktop.
Prueba de paso de mensajes con Socket.io
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
<html> | |
<head> | |
<title>Probando Socket.io</title> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect(); | |
socket.on('saludo', function(d) { | |
var text = prompt(d.info); | |
socket.emit('mensaje', text); | |
}); | |
socket.on('nuevoMensaje', function(msg) {alert(msg)}); | |
</script> | |
</head> | |
<body> | |
</body> | |
</html> |
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
var app = require('http').createServer(handler), | |
io = require('socket.io').listen(app), | |
fs = require('fs'); | |
app.listen(3000); | |
function handler(req, res) { | |
fs.readFile(__dirname + '/index.html', | |
function(err, data) { | |
if (err) { | |
res.writeHead(500); | |
return res.end('Error loading index'); | |
} | |
res.writeHead(200); | |
res.end(data); | |
}); | |
}; | |
io.sockets.on('connection', function(s) { | |
s.emit('saludo', {info:'Hola Bienvenido!\n Envia un Mensaje'}); | |
s.on('respuesta', function(d) {console.log(d);}); | |
s.on('mensaje', function(msg) { | |
s.broadcast.emit('nuevoMensaje', msg); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment