Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active February 6, 2016 04:59
Show Gist options
  • Select an option

  • Save stephenlb/4cf55f59152ab2bd1625 to your computer and use it in GitHub Desktop.

Select an option

Save stephenlb/4cf55f59152ab2bd1625 to your computer and use it in GitHub Desktop.
PubNub Mini-SDK for js13kgames.com - Look below for Usage Example.
(function(){
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Request URL
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
var requester = window.requester = function(setup) {
var xhr = new XMLHttpRequest()
, finished = false
, timeout = setup.timeout || 5000
, success = setup.success || function(){}
, fail = setup.fail || function(){};
// Cancel a Pending Request
function abort() {
if (finished) return;
xhr.abort && xhr.abort();
finish();
}
// Mark Request as Completed
function finish() {
finished = true;
}
// When a Request has a Payload
xhr.onload = function() {
if (finished) return;
finish();
var result;
try { result = JSON.parse(xhr.response) }
catch(e) { fail(xhr) }
if (result) success(result);
else fail(xhr);
result = null;
};
// When a Request has Failed
xhr.onabort = xhr.ontimeout = xhr.onerror = function() {
if (finished) return;
finish();
fail(xhr);
};
// Timeout and Aboart for Slow Requests
xhr.timer = setTimeout( function(){
if (finished) return;
abort();
fail(xhr);
}, timeout );
// Return Requester Object
return function(setup) {
var url = setup.url || 'https://ps.pubnub.com/time/0'
, headers = setup.headers || {}
, method = setup.method || 'GET'
, payload = setup.payload || null
, params = setup.params || setup.data || {}
, data = [];
// URL Parameters
for (var param in params)
data.push([ param, params[param] ].join('='));
// Start Request
finished = false;
xhr.timeout = timeout;
xhr.open(
method,
url + (data.length ? ('?' + data.join('&')) : ''),
true
);
// Headers
for (var header in headers)
xhr.setRequestHeader( header, headers[header] );
// Send Request
xhr.send(payload);
return {
xhr : xhr,
abort : abort
}
};
};
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Subscribe
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
var subscribe = window.subscribe = function(setup) {
var pubkey = setup.pubkey || 'demo'
, subkey = setup.subkey || 'demo'
, channel = setup.channel || 'a'
, timeout = setup.timeout || 290000
, timetoken = setup.timetoken || '0'
, message = setup.message || function(){}
, windy = setup.windowing || 1000
, windowing = 10
, stop = false
, url = ''
, origin = 'ps'+(Math.random()+'').split('.')[1]+'.pubnub.com';
// Requester Object
var request = requester({
timeout : timeout,
success : next,
fail : function(){ next() }
});
// Subscribe Loop
function next(payload) {
if (stop) return;
if (payload) {
timetoken = payload.t.t;
message(payload);
}
url = [
'https://', origin,
'/v2/subscribe/', subkey,
'/', channel,
'/0/', timetoken
].join('');
setTimeout( function() {
windowing = windy;
request({ url : url });
}, windowing );
}
// Cancel Subscription
function unsubscribe() {
stop = true;
}
// Start Subscribe Loop
next();
// Allow Cancelling Subscriptions
return {
unsubscribe : unsubscribe
};
};
})();
@stephenlb

Copy link
Copy Markdown
Author

PubNub js13kgames SDK Mini Edition Usage

Follow the basic usage below to receive and sending game events. Get your PubKey and SubKey by visiting http://www.pubnub.com/get-started/ to get started.

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Open TCP Socket for Receiving Game Events
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
subscribe({
    subkey    : 'YOUR_SUB_KEY'
,   pubkey    : 'YOUR_PUB_KEY'
,   channel   : 'game-channel'
,   message   : game_command
});

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Receive Events - Game Command and Control
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
function game_command(message) {
    data.m.forEach(function(message){
        console.log(message); // -- received event
    });
}

// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Send Events - Game Command and Control
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
function send( message, channel, pubkey, subkey ) {
    var msg = JSON.stringify(message);
    requester({
        timeout : 5000
    ,   fail    : function() {  }
    ,   success : function(data) {  }
    })({
        url : 'https://pubsub.pubnub.com/publish/'
              +pubkey+'/'+subkey+'/0/'+channel+'/0/'+msg
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment