Node realtime part

Download Node realtime part

Post on 10-Aug-2014

802 views

Category:

Art & Photos

1 download

Embed Size (px)

DESCRIPTION

 

TRANSCRIPT

<ul><li>MAKE WEB REAL RealTime Socket.io Caesar Chi http://about.me/clonn Saturday, April 19, 14 </li><li>Caesar Chi clonncd Saturday, April 19, 14 </li><li>http://www.ickr.com/photos/kent-chen/6612953485/sizes/l/in/photostream/ Saturday, April 19, 14 </li><li>REAL TIME? Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li> Saturday, April 19, 14 </li><li>http://www.ickr.com/photos/goldentime/4089413847/sizes/z/in/photostream/ Saturday, April 19, 14 </li><li> Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li> Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>http://socket.io/ Saturday, April 19, 14 </li><li>socket.ioinstall Saturday, April 19, 14 </li><li>Socket.io Socket.io -client Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>Chrome FireFox IE Mobile Saturday, April 19, 14 </li><li> WebSocket Adobe Flash Socket AJAX long polling AJAX multipart streaming Forever Iframe JSONP Polling Saturday, April 19, 14 </li><li>Simple Code https://gist.github.com/4049817 Saturday, April 19, 14 </li><li>var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); var socket = io.connect(); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); Saturday, April 19, 14 </li><li>var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs') var socketio = require('socket.io'), io; 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); }); } Saturday, April 19, 14 </li><li> Saturday, April 19, 14 </li><li>with Express Saturday, April 19, 14 </li><li>server = http.createServer(app); server.listen(app.get('port'), function(){ console.log("Express server listening on port " + app.get('port')); }); require('./io.server').io(server); Saturday, April 19, 14 </li><li>var socketio = require('socket.io'), io; exports.io = function (server) { io = socketio.listen(server); io.sockets.on('connection', function (socket) { socket.emit('init', { id: socket.id }); }); }; Saturday, April 19, 14 </li><li>var socket = io.connect(), socket.on('init', function (data) { concole.log(data); }); Saturday, April 19, 14 </li><li> Saturday, April 19, 14 </li><li>io.sockets.on('connection', function (socket) { socket.broadcast.emit('user connected'); }); () Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>io.sockets.on('connection', function (socket) { io.sockets.emit('user connected'); }); () Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li> https://github.com/LearnBoost/Socket.IO/wiki/Conguring- Socket.IO Saturday, April 19, 14 </li><li>io.configure('production', function(){ io.enable('browser client etag'); io.set('log level', 1); io.set('transports', [ 'websocket' , 'flashsocket' , 'htmlfile' , 'xhr-polling' , 'jsonp-polling' ]); }); io.configure('development', function(){ io.set('transports', ['websocket']); }); Saturday, April 19, 14 </li><li>io.configure('production', function(){ io.enable('browser client etag'); io.set('log level', 1); io.set('transports', [ 'websocket' , 'flashsocket' , 'htmlfile' , 'xhr-polling' , 'jsonp-polling' ]); }); io.configure('development', function(){ io.set('transports', ['websocket']); }); NODE_ENV=production node app.js Saturday, April 19, 14 </li><li>io.configure('production', function(){ io.enable('browser client etag'); io.set('log level', 1); io.set('transports', [ 'websocket' , 'flashsocket' , 'htmlfile' , 'xhr-polling' , 'jsonp-polling' ]); }); io.configure('development', function(){ io.set('transports', ['websocket']); }); NODE_ENV=production node app.js NODE_ENV=development node app.js Saturday, April 19, 14 </li><li> Saturday, April 19, 14 </li><li>// assuming io is the Socket.IO server object io.configure(function () { io.set("transports", ["xhr-polling"]); io.set("polling duration", 10); }); Saturday, April 19, 14 </li><li> https://github.com/LearnBoost/socket.io/wiki/Authorizing Saturday, April 19, 14 </li><li>handshake Saturday, April 19, 14 </li><li>Cookie Saturday, April 19, 14 </li><li>{ headers: req.headers // the headers of the request , time: (new Date) +'' // date time of the connection , address: socket.address() // remoteAddress and remotePort object , xdomain: !!headers.origin // was it a cross domain request? , secure: socket.secure // https connection , issued: +date // EPOCH of when the handshake was created , url: request.url // the entrance path of the request , query: data.query // the result of url.parse().query or a empty object } Saturday, April 19, 14 </li><li> Saturday, April 19, 14 </li><li>var io = require('socket.io').listen(80); io.configure(function (){ io.set('authorization', function (handshakeData, callback) { handshakeData.userData = { name: Caesar, age: 18 }; callback(null, true); // error first callback style }); }); User Connection Saturday, April 19, 14 </li><li>io.sockets.on('connection', function (socket) { console.log(socket.handshake.userData); }); User Data Saturday, April 19, 14 </li><li>NODE Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li>Saturday, April 19, 14 </li><li> https://github.com/clonn/socket-server https://github.com/clonn/comet-todo-list Saturday, April 19, 14 </li><li>https://gist.github.com/4049817 https://github.com/clonn/socket-server https://github.com/clonn/comet-todo-list Saturday, April 19, 14 </li></ul>