Permalink
Browse files

upgrading javascript modern to 3.4 with usage of common code from jav…

…ascript dir
  • Loading branch information...
1 parent c5ae915 commit fc7f95f731b20109a836485cf20ae011cfae0c7b @devendram devendram committed Jan 11, 2013
@@ -0,0 +1,59 @@
+# PubNub HTML5 Modern JavaScript Library
+
+For a faster PubNub load, use the PubNub HTML5 Modern JavaScript
+Library which is `CommonJS` and HTML5 `WebWorker` Ready.
+
+#### Supported Browsers:
+
+ - firefox/3.6'
+ - firefox/9.0'
+ - firefox/10.0'
+ - chrome/16.0'
+ - chrome/17.0'
+ - iexplore/9.0'
+ - safari/5.1'
+
+```html
+<script src=pubnub-3.3.js></script>
+<script>(function(){
+ // ----------------------------------
+ // INIT PUBNUB
+ // ----------------------------------
+ var pubnub = PUBNUB({
+ publish_key : 'PUBLISH_KEY_HERE',
+ subscribe_key : 'SUBSCRIBE_KEY_HERE',
+ ssl : false,
+ origin : 'pubsub.pubnub.com'
+ });
+
+ // ----------------------------------
+ // LISTEN FOR MESSAGES
+ // ----------------------------------
+ pubnub.subscribe({
+ restore : true,
+ connect : send_hello,
+ channel : 'my_channel',
+ callback : function(message) {
+ console.log(JSON.stringify(message));
+ },
+ disconnect : function() {
+ console.log("Connection Lost");
+ }
+ });
+
+ // ----------------------------------
+ // SEND MESSAGE
+ // ----------------------------------
+ function send_hello() {
+ pubnub.publish({
+ channel : 'my_channel',
+ message : { example : "Hello World!" },
+ callback : function(info) {
+ console.log(JSON.stringify(info));
+ }
+ });
+ }
+})();</script>
+
+```
+
@@ -0,0 +1,204 @@
+/* ---------------------------------------------------------------------------
+--------------------------------------------------------------------------- */
+
+/* ---------------------------------------------------------------------------
+PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks
+Copyright (c) 2011 PubNub Inc.
+http://www.pubnub.com/
+http://www.pubnub.com/terms
+--------------------------------------------------------------------------- */
+
+/* ---------------------------------------------------------------------------
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+--------------------------------------------------------------------------- */
+(function(){
+
+/**
+ * UTIL LOCALS
+ */
+var NOW = 1
+, URLBIT = '/'
+, PARAMSBIT = '&'
+, XHRTME = 310000;
+
+/**
+ * UTILITIES
+ */
+function unique() { return'x'+ ++NOW+''+(+new Date) }
+function rnow() { return+new Date }
+
+/**
+ * LOCAL STORAGE
+ */
+var db = (function(){
+ var ls = typeof localStorage != 'undefined' && localStorage;
+ return {
+ get : function(key) {
+ try {
+ if (ls) return ls.getItem(key);
+ if (document.cookie.indexOf(key) == -1) return null;
+ return ((document.cookie||'').match(
+ RegExp(key+'=([^;]+)')
+ )||[])[1] || null;
+ } catch(e) { return }
+ },
+ set : function( key, value ) {
+ try {
+ if (ls) return ls.setItem( key, value ) && 0;
+ document.cookie = key + '=' + value +
+ '; expires=Thu, 1 Aug 2030 20:00:00 UTC; path=/';
+ } catch(e) { return }
+ }
+ };
+})();
+
+
+/**
+ * CORS XHR Request
+ * ================
+ * xdr({
+ * url : ['http://www.blah.com/url'],
+ * success : function(response) {},
+ * fail : function() {}
+ * });
+ */
+function xdr( setup ) {
+ var xhr
+ , finished = function() {
+ if (loaded) return;
+ loaded = 1;
+
+ clearTimeout(timer);
+
+ try { response = JSON['parse'](xhr.responseText); }
+ catch (r) { return done(1); }
+
+ success(response);
+ }
+ , complete = 0
+ , loaded = 0
+ , timer = timeout( function(){done(1)}, XHRTME )
+ , fail = setup.fail || function(){}
+ , success = setup.success || function(){}
+ , done = function(failed) {
+ if (complete) return;
+ complete = 1;
+
+ clearTimeout(timer);
+
+ if (xhr) {
+ xhr.onerror = xhr.onload = null;
+ xhr.abort && xhr.abort();
+ xhr = null;
+ }
+
+ failed && fail();
+ };
+
+ // Send
+ try {
+ xhr = typeof XDomainRequest !== 'undefined' &&
+ new XDomainRequest() ||
+ new XMLHttpRequest();
+
+ xhr.onerror = xhr.onabort = function(){ done(1) };
+ xhr.onload = xhr.onloadend = finished;
+ xhr.timeout = XHRTME;
+ url = setup.url.join(URLBIT);
+ if (setup.data) {
+ var params = [];
+ url += "?";
+ for (key in setup.data) {
+ params.push(key+"="+setup.data[key]);
+ }
+ url += params.join(PARAMSBIT);
+ }
+ xhr.open( 'GET', url, true );
+ xhr.send();
+ }
+ catch(eee) {
+ done(0);
+ return xdr(setup);
+ }
+
+ // Return 'done'
+ return done;
+}
+
+
+
+/**
+ * LOG
+ * ===
+ * var list = grep( [1,2,3], function(item) { return item % 2 } )
+ */
+var log = function(){};
+
+/**
+ * EVENTS
+ * ======
+ * PUBNUB.events.bind( 'you-stepped-on-flower', function(message) {
+ * // Do Stuff with message
+ * } );
+ *
+ * PUBNUB.events.fire( 'you-stepped-on-flower', "message-data" );
+ * PUBNUB.events.fire( 'you-stepped-on-flower', {message:"data"} );
+ * PUBNUB.events.fire( 'you-stepped-on-flower', [1,2,3] );
+ *
+ */
+var events = {
+ 'list' : {},
+ 'unbind' : function( name ) { events.list[name] = [] },
+ 'bind' : function( name, fun ) {
+ (events.list[name] = events.list[name] || []).push(fun);
+ },
+ 'fire' : function( name, data ) {
+ each(
+ events.list[name] || [],
+ function(fun) { fun(data) }
+ );
+ }
+};
+
+/* =-====================================================================-= */
+/* =-====================================================================-= */
+/* =-========================= PUBNUB ===========================-= */
+/* =-====================================================================-= */
+/* =-====================================================================-= */
+
+function PN(setup) {
+ var SELF = {
+ // Expose PUBNUB Functions
+ 'xdr' : xdr,
+ 'db' : db,
+ 'each' : each,
+ 'map' : map,
+ 'events' : events
+ };
+ setup['db'] = db;
+ setup['xdr'] = xdr;
+ SELF.__proto__ = PN_API(setup);
+ SELF.ready();
+ return SELF;
+}
+
+typeof module !== 'undefined' && (module.exports = PN) ||
+typeof exports !== 'undefined' && (exports.PN = PN) || (PUBNUB = PN);
+
+})();
Oops, something went wrong.
Binary file not shown.
@@ -0,0 +1,84 @@
+<!doctype html>
+<html>
+<head>
+ <title>PubNub JavaScript CommonJS</title>
+ <link
+ rel=stylesheet
+ href=http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css
+ >
+</head>
+<body><div class=container>
+
+</div>
+<script src=../../../javascript/3.4/pubnub-3.4-common.js></script>
+<script src=../pubnub-3.4.js></script>
+<script>(function() {
+ // ----------------------------------------------
+ // 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);
+ }
+
+ // ----------------------------------------------
+ // Send Request Finished with Status
+ // ----------------------------------------------
+ function log(message) {
+ console.log(message);
+ }
+
+})();</script>
+</body>
+</html>
Oops, something went wrong.

0 comments on commit fc7f95f

Please sign in to comment.