Last active
December 15, 2015 13:29
-
-
Save alejandro/5268098 to your computer and use it in GitHub Desktop.
A un unico usuario
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></title> | |
</head> | |
<body> | |
<script type="text/javascript" src="/socket.io/socket.io.js"></script> | |
<script type="text/javascript"> | |
var ws = io.connect() | |
ws.on('msg', function (msg){ | |
document.writeln('new msg from: ' + msg.from) | |
document.writeln(' ' + msg.text) | |
}) | |
ws.on('user', function (msg){ | |
document.writeln('new user connected: ' + msg) | |
}) | |
</script> | |
<!-- Abres consola y escribes: | |
ws.emit('msg', {id: "ID del usuario a quien enviar", text: "msg a enviar"}) | |
y listo :) | |
--> | |
</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
// npm install socket.io express | |
var http = require('http') | |
var express = require('express') | |
var sio = require('socket.io') | |
var server = express() | |
server.use(express.static(__dirname)) | |
http.createServer(server).listen(3000, function (){ | |
var ws = sio.listen(this); | |
console.log('Servidor listo en %d', this.address().port) | |
ws.on('connection', function (socket){ | |
socket.on('msg', function (msg){ | |
msg.from = socket.id | |
ws.sockets.socket(msg.id).emit('msg', msg) | |
}) | |
socket.broadcast.emit('user', socket.id) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment