Skip to content

Instantly share code, notes, and snippets.

@cronopio
Created November 25, 2011 21:16
Show Gist options
  • Save cronopio/1394428 to your computer and use it in GitHub Desktop.
Save cronopio/1394428 to your computer and use it in GitHub Desktop.
Prueba de paso de mensajes con Socket.io
<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>
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