MAKE WEB REAL
    RealTime Socket.io




                  Caesar Chi
                  http://about.me/clonn
http://www.flickr.com/photos/kent-chen/6612953485/sizes/l/in/photostream/
REAL TIME?
考慮?
http://www.flickr.com/photos/goldentime/4089413847/sizes/z/in/photostream/
效能
極限
極限
極限
開發成本
中國
組
模
組
模
http://socket.io/
install socket.io
•Socket.io

•Socket.io
-client
•Chrome

•FireFox

•IE

•Mobile
•   WebSocket

•   Adobe® Flash® Socket

•   AJAX long polling

•   AJAX multipart streaming

•   Forever Iframe

•   JSONP Polling
Simple Code
https://gist.github.com/4049817
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' });
   });
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);
    });
}
反饋式修改
with Express
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);




<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script src="/javascripts/io.client.js"></script>
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
    });
  });
};
 var socket = io.connect(),

 socket.on('init', function (data) {
   concole.log(data);
 });
廣播事件
io.sockets.on('connection', function (socket) {
  socket.broadcast.emit('user connected');
});

廣播事件(沒有自己)
io.sockets.on('connection', function (socket) {
  io.sockets.emit('user connected');
});

 廣播事件(有自己)
設定事件
https://github.com/LearnBoost/Socket.IO/wiki/Configuring-
                        Socket.IO
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']);
});
io.configure('production', function(){
  io.enable('browser client etag');
  io.set('log level', 1);

  io.set('transports', [
    'websocket'
  , 'flashsocket'           NODE_ENV=production node app.js
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);
});

io.configure('development', function(){
  io.set('transports', ['websocket']);
});
io.configure('production', function(){
  io.enable('browser client etag');
  io.set('log level', 1);

  io.set('transports', [
    'websocket'
  , 'flashsocket'           NODE_ENV=production node app.js
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);
});

io.configure('development', function(){
  io.set('transports', ['websocket']);
});
                           NODE_ENV=development node app.js
例如
// assuming io is the Socket.IO server object
io.configure(function () {
  io.set("transports", ["xhr-polling"]);
  io.set("polling duration", 10);
});
授權設定
https://github.com/LearnBoost/socket.io/wiki/Authorizing
handshake
Cookie
{
   headers: req.headers
// <Object> the headers of the request
 , time: (new Date) +''
// <String> date time of the connection
 , address: socket.address()
// <Object> remoteAddress and remotePort object
 , xdomain: !!headers.origin
// <Boolean> was it a cross domain request?
 , secure: socket.secure
// <Boolean> https connection
 , issued: +date
// <Number> EPOCH of when the handshake was created
 , url: request.url
// <String> the entrance path of the request
 , query: data.query
// <Object> the result of url.parse().query or a empty object
}
驗證、串接資料
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
io.sockets.on('connection', function (socket) {

      console.log(socket.handshake.userData);
});




                      User Data 取得
NODE 到底是什麼?
範例

• https://github.com/clonn/socket-server

• https://github.com/clonn/comet-todo-list
https://gist.github.com/4049817
https://github.com/clonn/socket-server
https://github.com/clonn/comet-todo-list

Node worshop Realtime - Socket.io

Editor's Notes

  • #2 \n
  • #3 Node &amp;#x958B;&amp;#x767C;&amp;#x76EE;&amp;#x524D;&amp;#x5C31;&amp;#x50CF;&amp;#x662F;&amp;#x7A4D;&amp;#x6728;&amp;#x4E00;&amp;#x6A23;&amp;#xFF0C;&amp;#x9700;&amp;#x8981;&amp;#x81EA;&amp;#x5DF1;&amp;#x62FC;&amp;#x6E4A;&amp;#x5F88;&amp;#x591A;&amp;#x6771;&amp;#x897F;&amp;#x90FD;&amp;#x8981;&amp;#x9760;&amp;#x81EA;&amp;#x5DF1;&amp;#x53BB;&amp;#x7D44;&amp;#x5408;&amp;#xFF0C;&amp;#x5BE6;&amp;#x9A57;&amp;#xFF0C;&amp;#x8A0E;&amp;#x8AD6;&amp;#x3002;\n\n\n
  • #4 \n
  • #5 \n
  • #6 \n
  • #7 \n
  • #8 \n
  • #9 \n
  • #10 CPU\n
  • #11 MEM\n\n
  • #12 user \n
  • #13 \n
  • #14 &amp;#x524D;&amp;#x5F8C;&amp;#x7AEF;&amp;#x90FD;&amp;#x4E00;&amp;#x8D77;&amp;#x5BEB; JavaScript\n
  • #15 \n
  • #16 \n
  • #17 \n
  • #18 \n
  • #19 client, server , socket.io version\n
  • #20 \n
  • #21 \n
  • #22 \n
  • #23 \n
  • #24 \n
  • #25 read file parts\n
  • #26 \n
  • #27 \n
  • #28 \n
  • #29 \n
  • #30 \n
  • #31 \n
  • #32 \n
  • #33 \n
  • #34 \n
  • #35 \n
  • #36 \n
  • #37 \n
  • #38 \n
  • #39 \n
  • #40 \n
  • #41 \n
  • #42 \n
  • #43 \n
  • #44 \n
  • #45 \n
  • #46 \n
  • #47 \n
  • #48 \n
  • #49 \n
  • #50 \n
  • #51 \n
  • #52 &amp;#x975E;&amp;#x963B;&amp;#x585E;&amp;#x3001;&amp;#x4E8B;&amp;#x4EF6;&amp;#x9A45;&amp;#x52D5;&amp;#xFF1F;&amp;#x8DDF;&amp;#x5927;&amp;#x5BB6;&amp;#x8AAA;&amp;#x660E;&amp;#x4E00;&amp;#x6B21;\n
  • #53 &amp;#x975E;&amp;#x963B;&amp;#x585E;&amp;#x3001;&amp;#x4E8B;&amp;#x4EF6;&amp;#x9A45;&amp;#x52D5;&amp;#xFF1F;&amp;#x8DDF;&amp;#x5927;&amp;#x5BB6;&amp;#x8AAA;&amp;#x660E;&amp;#x4E00;&amp;#x6B21;\n
  • #54 &amp;#x975E;&amp;#x963B;&amp;#x585E;&amp;#x3001;&amp;#x4E8B;&amp;#x4EF6;&amp;#x9A45;&amp;#x52D5;&amp;#xFF1F;&amp;#x8DDF;&amp;#x5927;&amp;#x5BB6;&amp;#x8AAA;&amp;#x660E;&amp;#x4E00;&amp;#x6B21;\n
  • #55 \n
  • #56 \n