Skip to content

Commit

Permalink
added ruby example with content commander to push HTML content on dem…
Browse files Browse the repository at this point in the history
…and.
  • Loading branch information
stephenlb committed May 9, 2012
1 parent f379a1d commit dfd3951
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 4 deletions.
29 changes: 29 additions & 0 deletions ruby/commander/commander.rb
@@ -0,0 +1,29 @@
require '../lib/pubnub'

## -----------------------------------------
## SETUP API KEYS
## -----------------------------------------
publish_key = "09e02e4d-5508-44fb-a615-8da69289b73c"
subscribe_key = "a71204b3-ca89-11df-ba32-cfcef4a2b967"
secret_key = ""

## -----------------------------------------
## Create Pubnub Client API (INITIALIZATION)
## -----------------------------------------
pubnub = Pubnub.new( publish_key, subscribe_key, secret_key, false )

## ----------------------
## Send Message (PUBLISH)
## ----------------------
while 1
puts('Sending a message with publish() To Content Viewer')
info = pubnub.publish({
'channel' => 'content_commander',
'message' => {
'name' => 'arb_html',
'data' => "Current Time: <h1>" + Time.now.inspect + "</h1>"
}
})
sleep(1.0)
end

55 changes: 55 additions & 0 deletions ruby/commander/viewer.html
@@ -0,0 +1,55 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Content Commander </title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" />
</head>
<body>
<div data-role="page" id="main">
<div data-theme="a" data-role="header">
<h3>
Content Commander - Viewer
</h3>
</div><!-- /header -->
<div data-role="content" id="viewer_box" class="box left">
<div id="content"></div>
</div><!-- /content #viewer_box" -->
</div>

<!-- JQUERY -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script>

<!-- PUBNUB -->
<div sub-key="a71204b3-ca89-11df-ba32-cfcef4a2b967" ssl="off" origin="pubsub.pubnub.com" id="pubnub"></div>
<script src="http://cdn.pubnub.com/pubnub-3.1.min.js"></script>
<script>(function(){
$(document).data('uuid', (((1+Math.random())*0x10000)|0).toString(16).substring(1));

PUBNUB.events.bind('arb_html', function(html) {
$("#content").remove();
$("#viewer_box").append("<div id='content'>"+ html + "</div>");
});

PUBNUB.subscribe({
channel : "content_commander",
restore : false,
callback : function(message) {
if (message.name) {
PUBNUB.events.fire(message.name, message.data);
}
},
disconnect : function() {
console.log("Connection Lost.");
},
reconnect : function() {
console.log("And we're Back!");
}

});

})();</script>
</body>
</html>
8 changes: 4 additions & 4 deletions ruby/lib/pubnub.rb
Expand Up @@ -12,7 +12,7 @@
require 'digest/md5'
require 'open-uri'
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
require 'pp'

Expand Down Expand Up @@ -43,10 +43,10 @@ def initialize( publish_key, subscribe_key, secret_key, ssl_on = false)
@origin = 'http://' + @origin
end

uri = URI.parse(@origin)
http = Net::HTTP.new(uri.host, uri.port)
uri = URI.parse(@origin)
http = Net::HTTP.new( uri.host, uri.port )
http.use_ssl = ssl_on
@connection = http.start()
@connection = http.start()
end

#**
Expand Down

0 comments on commit dfd3951

Please sign in to comment.