21. Socket.io и Nordstream
var http = require('http'),
io = require('socket.io')
server = http.createServer(function(req, res){
res.writeHeader(200, {'Content-Type': 'text/html'});
res.writeBody('<h1>Hello world</h1>');
res.finish();
});
// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
// new client is here!
client.on('message', function(){ … })
client.on('disconnect', function(){ … })
});
22. var socket = new io.Socket();
socket.on('connect', function(){
socket.send('hi!');
})
socket.on('message', function(data){
alert(data);
})
socket.on('disconnect', function(){})
23. Nordstream
var connections = 0;
var nodestream = io.listen(app).nodestream()
.on('connect', function(){
connections++;
this.emit('connections', connections);
})
.on('disconnect', function(){
connections--;
this.emit('connections', connections);
});
:realtime(repaint: 'connections', local: 'connections')
.connections
- if (connections > 1)
p #{connections} people are editing right now
- else
p You're all alone, loser