Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit f868625d1d5053a4ca413c73def90f5a0424e5a3 @shaokun committed Sep 5, 2010
Showing with 129 additions and 0 deletions.
  1. +19 −0 README
  2. +44 −0 client.html
  3. +19 −0 jquery.min.js
  4. +47 −0 server.rb
@@ -0,0 +1,19 @@
+h1. About
+
+An server-client example of using Twitter Stream API and WebSocket.
+
+
+h1. Requirement
+
+* Safari 4.x
+* Chrome
+
+
+h1. How to use
+
+0) pico server.rb
+ change USERNAME, PASSWORD to your twitter account
+1) ruby server.rb
+3) open client.html through Safari 4.x or Chrome, you should see tweets flow into your browser.
+4) if you like, you could change the keyword from 'iphone' to others.
+filter_url = 'http://stream.twitter.com/1/statuses/filter.json?track=iphone'
@@ -0,0 +1,44 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+ <head>
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
+
+ <script type="text/javascript" src='jquery.min.js'></script>
+
+ <script type="text/javascript" charset="utf-8">
+ $(document).ready(function(){
+ if(!("WebSocket" in window)) {
+ alert("Sorry, the build of your browser does not support WebSockets. Please use latest Chrome or Webkit nightly");
+ return;
+ }
+
+ ws = new WebSocket("ws://localhost:8080/");
+
+ ws.onmessage = function(evt) {
+ var p = $("<div class='tweet' style='display:none'>" + evt.data + "</div>");
+ if($('#tweets div.tweet').size() > 15) {
+ $('#tweets div.tweet:last').slideDown(100, function() {
+ $(this).remove();
+ });
+ }
+ $('#tweets').prepend(p);
+ p.slideDown(140);
+ };
+
+ ws.onclose = function() {
+ alert("socket closed");
+ };
+ ws.onopen = function() {
+ alert("connected...");
+ };
+ });
+ </script>
+ </head>
+
+
+ <body>
+ <div id="tweets"></div>
+ </body>
+
+</html>
@@ -0,0 +1,47 @@
+require 'rubygems'
+require 'em-websocket'
+require 'em-http'
+require 'json'
+
+USERNAME = 'YOURUSERNAME'
+PASSWORD = 'YOURPASSWORD'
+
+sample_url = 'http://stream.twitter.com/1/statuses/sample.json'
+filter_url = 'http://stream.twitter.com/1/statuses/filter.json?track=iphone'
+
+
+EM.run do
+ channel = EM::Channel.new
+ http = EventMachine::HttpRequest.new(filter_url).get :head => { 'Authorization' => [ USERNAME, PASSWORD ] }
+
+ buffer = ""
+
+ http.stream do |chunk|
+ buffer += chunk
+ while line = buffer.slice!(/.+\r?\n/)
+ tweet = JSON.parse(line)
+ next unless tweet['text']
+
+ msg = "<span style='font-weight: bold; color: blue'>#{tweet['user']['screen_name']}</span>:&nbsp;&nbsp;&nbsp;&nbsp;#{tweet['text']}"
+
+ puts "\t#{msg}"
+ channel << msg
+ end
+ end
+
+ EventMachine::WebSocket.start(:host => "localhost", :port => 8080) do |ws|
+ ws.onopen do
+ puts "WebSocket opened"
+
+ @sid = channel.subscribe do |msg|
+ ws.send msg
+ end
+ end
+
+ ws.onclose do
+ channel.unsubscribe @sid
+
+ puts "WebSocket closed"
+ end
+ end
+end

0 comments on commit f868625

Please sign in to comment.