WebSocket 实时推特流
Upcoming SlideShare
Loading in...5
×
 

WebSocket 实时推特流

on

  • 2,487 views

Example Code:

Example Code:
http://github.com/shaokun/twlive

Statistics

Views

Total Views
2,487
Views on SlideShare
2,487
Embed Views
0

Actions

Likes
1
Downloads
44
Comments
1

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • the example code is here:
    http://github.com/shaokun/twlive
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

WebSocket 实时推特流 WebSocket 实时推特流 Presentation Transcript

  • WebSocket 实时推特流 [email_address]
  • 需要了解的概念
    • HTML 5 WebSocket
    • EventMachine (EM)
    • HTTP Stream
  • Browser HTML 5 WebSocket EM::WebSocket EM::Channel EM::HttpRequest Twitter Stream Browser HTML 5 WebSocket Browser HTML 5 WebSocket EM::WebSocket EM::WebSocket Publish Subscribe TCP Channel
  • HTML 5 WebSocket
    • 通过 Web 的 Socket
    • 全双工的通讯通道
    • 双向通讯
    • 更小的延迟,实时的
    • < script type = &quot;text/javascript&quot; >
    • // 创建 WebSocket 对象很容易
    • ws = new WebSocket ( &quot;ws://localhost:8080/&quot; );
    • </ script >
  •  
  • ws . onopen = function () {...}; ws . onclose = function () {...}; ws . onerror = function (event) {...}; ws . onmessage = function (event) {...};
  • ws . send (msg); ws . close ();
  • 支持的 Browsers
    • Chrome
    • Safari 4.x
    • Firefox 3.7
    • IE ??? github.com/gimite/web-socket-js
  • EventMachine simple, fast, event-driven I/O library for Ruby
  • EM::WebSocket
    • EM :: WebSocket .start( :host => &quot;localhost&quot; , :port => 8080) do | ws |
    • ws.onopen do
    • end
    • ws.onclose do
    • end
    • end
  • Publish/Subscribe EM::Channel
    • channel = EM :: Channel . new
    • channel << &quot;a new message&quot;
    • sid = channel.subscribe do | msg |
    • puts msg # should output ‘a new message’
    • end
  • HTTP Stream
  • Twitter Stream API
    • http://stream.twitter.com/1/statuses/sample.json
    • http://stream.twitter.com/1/statuses/filter.json?track=???
    • url = 'http://stream.twitter.com/1/statuses/sample.json'
    • buffer = &quot;&quot;
    • http.stream do | chunk |
    • buffer += chunk
    • while line = buffer.slice!(/.+ ? /)
    • tweet = JSON .parse(line)
    • next unless tweet[ 'text' ]
    • msg = &quot;#{tweet['user']['screen_name']}: #{tweet['text']}&quot;
    • channel << msg
    • end
    • end
  • Browser HTML 5 WebSocket EM::WebSocket EM::Channel EM::HttpRequest Twitter Stream Browser HTML 5 WebSocket Browser HTML 5 WebSocket EM::WebSocket EM::WebSocket Publish Subscribe TCP Channel
  • 问答 ??? QA [email_address]