Skip to content

Commit

Permalink
upgraded pubnub javascript modern with webworker example.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenlb committed Jun 5, 2012
1 parent 922dcc6 commit 60223a8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 77 deletions.
12 changes: 10 additions & 2 deletions javascript-modern/pubnub-3.1.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ function encode(path) {
} ).join('');
}

/**
* LOG
* ===
* var list = grep( [1,2,3], function(item) { return item % 2 } )
*/
var log = function(){};


/**
* EVENTS
* ======
Expand Down Expand Up @@ -214,7 +222,7 @@ function xdr( setup ) {

// Send
try {
xhr = window.XDomainRequest &&
xhr = typeof XDomainRequest !== 'undefined' &&
new XDomainRequest() ||
new XMLHttpRequest();

Expand Down Expand Up @@ -272,7 +280,7 @@ function PN(setup) {
0, limit
],
success : function(response) { callback(response) },
fail : function(response) { log(response) }
fail : function(response) { callback(response) }
});
},

Expand Down
77 changes: 5 additions & 72 deletions javascript-modern/tests/webworker.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,81 +7,14 @@
href=http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css
>
</head>
<body><div class=container>

</div>
<!--<script src=../pubnub-3.1.js></script>-->
<body><div class=container></div>
<script>(function() {

var pubnub = new Worker("./webworker.js");

return;

// ----------------------------------------------
// INIT
// ----------------------------------------------
var channel = 'my_channel'
, pubnub = PUBNUB({
publish_key : 'demo',
subscribe_key : 'demo',
ssl : false,
origin : 'pubsub.pubnub.com'
});

// ----------------------------------------------
// Establish a Connection
// ----------------------------------------------
log('Opening a Connection.');
pubnub.subscribe({
channel : channel,
connect : ready,
callback : receive
});

// ----------------------------------------------
// Connection Is Open Now and Ready
// ----------------------------------------------
function ready() {
log('Connection Established.');
send('hello');
}

// ----------------------------------------------
// Received Message
// ----------------------------------------------
function receive(message) {
log('Received a Message.');
log(message);
log('Closing Connection');
pubnub.unsubscribe({ channel : channel });
}

// ----------------------------------------------
// Send a Message
// ----------------------------------------------
function send(message) {
log('Starting to Send a Message.');
pubnub.publish({
channel : channel,
message : message,
callback : send_complete
});
}

// ----------------------------------------------
// Send Request Finished with Status
// ----------------------------------------------
function send_complete(details) {
log('Message Sent Done.');
log(details);
}
var worker = new Worker("./webworker.js")

// ----------------------------------------------
// Send Request Finished with Status
// ----------------------------------------------
function log(message) {
console.log(message);
}
worker.onmessage = function(evt){
console.log(JSON.stringify(evt.data))
};

})();</script>
</body>
Expand Down
27 changes: 24 additions & 3 deletions javascript-modern/tests/webworker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@

(function(){
1;

"use strict"

importScripts('../pubnub-3.1.js')

var app = PUBNUB({
publish_key : 'demo',
subscribe_key : 'demo'
})

app.publish({
channel : 'my_channel',
message : 'It Works!',
callback : function(info) {
postMessage(info)
app.history({
channel : 'my_channel',
limit : 1,
message : 123,
callback : postMessage
})
}
})

})();
//postMessage('test');

0 comments on commit 60223a8

Please sign in to comment.