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!(/.+\r?\n/) 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]

WebSocket 实时推特流

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