Just an example for the participants about how to use websockets on node.js. First example using ws
for server and WebSocket()
API of Chrome and second example using socket.io
Created
November 24, 2012 17:49
-
-
Save cronopio/4140670 to your computer and use it in GitHub Desktop.
BogotaJS
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 WebSocketServer = require('ws').Server | |
, MyServer = new WebSocketServer({port: 3000}); | |
MyServer.on('connection', function(socket) { | |
socket.on('message', function(message) { | |
console.log('Recibido: %s', message); | |
}); | |
setInterval(function () { | |
socket.send('¿Sigues mirando?'); | |
}, 4000) | |
}); | |
console.log('Demo corriendo'); |
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') | |
// Transportes soportados | |
// websocket, htmlfile, xhr-polling, jsonp-polling | |
io.set('transports', ['websocket']) | |
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.html'); | |
} | |
res.writeHead(200); | |
res.end(data); | |
}); | |
} | |
io.sockets.on('connection', function (socket) { | |
socket.emit('news', { noticia: 'ninguna' }); | |
socket.on('my other event', function (data) { | |
console.log(data); | |
}); | |
var mensajes = 1; | |
setInterval(function () { | |
socket.emit('news', { noticia: mensajes }); | |
mensajes++; | |
}, 4000); | |
}); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>BogotaJS</title> | |
<script src="/socket.io/socket.io.js"></script> | |
<script> | |
var socket = io.connect(); | |
var aviso = document.createElement('p'); | |
aviso.innerHTML = 'Ninguna noticia hasta ahora...'; | |
socket.on('news', function (data) { | |
console.log(data); | |
aviso.innerHTML = 'Recibidas ' + data.noticia + ' noticias'; | |
socket.emit('my other event', { my: 'data' }); | |
}); | |
window.onload = function () { document.body.appendChild(aviso); }; | |
</script> | |
</head> | |
<body> | |
<a href="http://www.bogotajs.com/">BogotaJS</a> | |
<p>WebSockets demo by <a href="http://github.com/cronopio">cronopio</a>.</p> | |
</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
{ | |
"name": "demowebsockets", | |
"version": "0.0.0-1", | |
"description": "un demo mas", | |
"main": "demo02.js", | |
"dependencies": { | |
"socket.io": "~0.9.11", | |
"ws": "~0.4.23" | |
}, | |
"devDependencies": {}, | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "node demo02.js" | |
}, | |
"repository": "", | |
"author": "cronopio", | |
"license": "BSD", | |
"subdomain": "bogotajs", | |
"engines": { | |
"node": "0.8.x" | |
} | |
} |
Dependencies
ws
: https://github.com/einaros/ws and http://einaros.github.com/ws/socket.io
: http://socket.io/ and http://socket.io/#how-to-use
OJO Si su navegador no tiene console.log
el JS del cliente reventara
Y por favor no usen setInterval()
en la vida real
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I deployed
demo02.js
to nodejitsuhttp://bogotajs.jit.su or http://bogotajs.nodejitsu.com