20. BAJO NIVEL
1 var sys = require('sys'),
2 WebSocket = require('../websockets');
3
4 function TimeJS () {
5 var self = this;
6
7 this.websocket = new WebSocket.Server({resource: '/time', host: 'localhost'});
8 this.connections = new Array();
9
10 this.websocket.addListener('connect', function (server, connection) {
11 self.connections[connection.socket.fd] = "";
12 });
13 this.websocket.addListener('disconnect', function (server, connection) {
14 clearInterval(self.connections[connection.socket.fd]);
15 self.connections.splice(self.connections.indexOf(connection.socket.fd), 1);
16 });
17 this.websocket.addListener('receive', function (data, connection) {
18 if (data == 'start') {
19 self.connections[connection.socket.fd] = setInterval(function () {
20 connection.write(JSON.stringify({time: new Date().toString()}));
21 }, 1000);
22 }
23 });
24 }
25
26 new TimeJS();
https://github.com/jackyyll/nodejs-websockets
21. Faye is an easy-to-use publish-subscribe
messaging system based on the Bayeux
protocol.
It provides message servers for Node.js and Rack,
and clients for use in Node and Ruby programs
http://faye.jcoglan.com
22. INICIAR UN SERVIDOR
1 var Faye = require('faye'),
2 server = new Faye.NodeAdapter({mount: '/'});
3
4 server.listen(8000);
23. CREAR UN CLIENTE
5 var client = new Faye.Client('http://localhost:8000/');
6
7 client.subscribe('/messages', function(message) {
8 alert('Got a message: ' + message.text);
9 });