Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding here_now and presence features, with unit tests using nodeunit…

…, also added a hook in subscribe function to exit the recursive call if required
  • Loading branch information...
commit ff34e50e587f06913432192cd584f28a8bd1e96f 1 parent 76dfb09
devendram devendram authored
Showing with 126 additions and 16 deletions.
  1. +62 −8 nodejs/3.2/pubnub.js
  2. +64 −8 nodejs/3.2/tests/unit-test.js
70 nodejs/3.2/pubnub.js
View
@@ -37,6 +37,7 @@ var NOW = 1
, http = require('http')
, https = require('https')
, URLBIT = '/'
+, QUERYBIT = '&'
, XHRTME = 310000
, XORIGN = 1;
@@ -112,7 +113,9 @@ function xdr( setup ) {
failed = 1;
(setup.fail||function(){})(e);
};
-
+ if (setup.query && setup.query.length > 0) {
+ url = url + '?' + setup.query.join(QUERYBIT);
+ }
try {
(ssl ? https : http).get( {
host : origin,
@@ -150,7 +153,18 @@ exports.init = function(setup) {
, SSL = setup.ssl
, ORIGIN = setup.origin || 'pubsub.pubnub.com'
, CHANNELS = {}
+ , UUID = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
+ var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
+ return v.toString(16);
+ })
, PN = {
+
+
+ uuid : uuid = function(callback) {
+ if (callback)
+ callback(UUID);
+ return UUID;
+ },
/*
PUBNUB.history({
channel : 'my_chat_channel',
@@ -250,7 +264,7 @@ exports.init = function(setup) {
callback : function(message) { console.log(message) }
});
*/
- 'subscribe' : function( args, callback ) {
+ 'subscribe' : subscribe = function( args, callback ) {
var channel = args['channel']
, callback = callback || args['callback']
@@ -289,6 +303,9 @@ exports.init = function(setup) {
encode(channel),
'0', timetoken
],
+ query : [
+ 'uuid=' + encode(uuid())
+ ],
fail : function() {
// Disconnect
if (!disconnected) {
@@ -301,27 +318,27 @@ exports.init = function(setup) {
});
},
success : function(messages) {
- var stop = false;
+ var ret = {stop: false};
if (!CHANNELS[channel].connected) return;
// Connect
if (!connected) {
connected = 1;
- connect();
+ ret = connect();
}
// Reconnect
if (disconnected) {
disconnected = 0;
- reconnect();
+ ret = reconnect();
}
messages[0].forEach(function(msg) {
- stop = callback( msg, messages );
+ ret = callback( msg, messages );
});
timetoken = messages[1];
- if(!stop)
+ if(!ret.stop)
timeout( pubnub, 10 );
}
});
@@ -330,8 +347,45 @@ exports.init = function(setup) {
// Begin Recursive Subscribe
pubnub();
},
- };
+ /*
+ PUBNUB.presence({
+ channel : 'my_chat'
+ callback : function(message) { console.log(message) }
+ });
+ */
+ 'presence' : function( args, callback ) {
+ args['channel'] = args['channel'] + '-pnpres';
+ subscribe(args,callback );
+ },
+ /*
+ PUBNUB.here_now({
+ channel : 'my_chat_channel',
+ callback : function(messages) { console.log(messages) }
+ });
+ */
+ 'here_now' : function( args, callback ) {
+ var callback = args['callback']
+ , channel = args['channel'];
+ // Make sure we have a Channel
+ if (!channel) return log('Missing Channel');
+ if (!callback) return log('Missing Callback');
+
+ // Send Message
+ xdr({
+ ssl : SSL,
+ url : [
+ 'v2',
+ 'presence',
+ 'sub-key', SUBSCRIBE_KEY,
+ 'channel', encode(channel),
+ ],
+ origin : ORIGIN,
+ success : callback,
+ fail : function(response) { log(response) }
+ });
+ },
+ };
return PN;
}
exports.unique = unique
72 nodejs/3.2/tests/unit-test.js
View
@@ -1,4 +1,4 @@
-var PUBNUB, channel, history_test, nodeunit, publish_dummy, publish_test, pubnub, subscribe_test, time_test, uuid_test;
+var PUBNUB, channel, here_now_test, history_test, nodeunit, presence_test, publish_dummy, publish_test, pubnub, run_dummy_subscribe, subscribe_test, time_test, uuid_test;
PUBNUB = require('../pubnub');
@@ -43,7 +43,7 @@ time_test = function(test) {
uuid_test = function(test) {
test.expect(1);
- return pubnub.time(function(uuid) {
+ return pubnub.uuid(function(uuid) {
test.ok(uuid);
test.done();
});
@@ -51,7 +51,6 @@ uuid_test = function(test) {
history_test = function(test) {
test.expect(2);
- console.log('history test');
return pubnub.history({
limit: 1,
channel: channel,
@@ -72,14 +71,69 @@ subscribe_test = function(test) {
connect: function() {
return publish_dummy(test_channel);
},
- message: {
- test: "test"
- },
callback: function(message) {
test.ok(message);
test.ok(message.test === "test");
test.done();
- return true;
+ return {stop: true};
+ }
+ });
+};
+
+run_dummy_subscribe = function(channel) {
+ var pubnub = PUBNUB.init({
+ publish_key: 'demo',
+ subscribe_key: 'demo'
+ });
+ return pubnub.subscribe({
+ channel: channel,
+ connect: function() {
+ return {stop: true};
+ },
+ callback: function() {
+ return {stop: true};
+ }
+ });
+};
+
+presence_test = function(test) {
+ var test_channel;
+ test_channel = 'channel-' + PUBNUB.unique();
+ test.expect(3);
+ return pubnub.presence({
+ channel: test_channel,
+ connect: function() {
+ run_dummy_subscribe(test_channel);
+ },
+ callback: function(message) {
+ test.ok(message);
+ test.ok(message.action === "join");
+ test.ok(message.occupancy === 1);
+ test.done();
+ return {stop: true};
+ }
+ });
+};
+
+here_now_test = function(test) {
+ var test_channel;
+ test_channel = 'channel-' + PUBNUB.unique();
+ test.expect(2);
+ return pubnub.subscribe({
+ channel: test_channel,
+ connect: function() {
+ pubnub.here_now({
+ channel: test_channel,
+ callback: function(message) {
+ test.ok(message);
+ test.ok(message.occupancy === 1);
+ publish_dummy(test_channel);
+ }
+ });
+ },
+ callback: function(message) {
+ test.done();
+ return {stop: true};
}
});
};
@@ -89,5 +143,7 @@ module.exports = {
"History Test": history_test,
"Time Test": time_test,
"UUID Test": uuid_test,
- "Subscribe Test": subscribe_test
+ "Subscribe Test": subscribe_test,
+ "Presence Test": presence_test,
+ "Here Now Test": here_now_test
};
Please sign in to comment.
Something went wrong with that request. Please try again.