Permalink
Browse files

made POST for PUBLISH method in Node.JS

  • Loading branch information...
1 parent 1f7539e commit 0b682da71f202e4389a02b708e3d23290b97f232 @stephenlb stephenlb committed Feb 28, 2014
Showing with 74 additions and 21 deletions.
  1. +25 −0 node.js/examples/publish.js
  2. +7 −1 node.js/examples/usage.js
  3. +21 −10 node.js/pubnub.js
  4. +21 −10 node.js/unassembled/platform.js
@@ -0,0 +1,25 @@
+/* ---------------------------------------------------------------------------
+
+ Init PubNub and Get your PubNub API Keys:
+ http://www.pubnub.com/account#api-keys
+
+--------------------------------------------------------------------------- */
+
+var pubnub = require("./../pubnub.js").init({
+ publish_key : "demo",
+ subscribe_key : "demo"
+});
+
+
+/* ---------------------------------------------------------------------------
+Listen for Messages
+--------------------------------------------------------------------------- */
+pubnub.publish({
+ channel : "my_channel",
+ message : ["askldjflk$%^&&^asjd"],
+ callback : log,
+ error : retry
+});
+
+function log(e) { console.log(e) }
+function retry() { console.log('retry?') }
@@ -14,7 +14,8 @@ var network = pubnub.init({
});
var delivery_count = 0;
-var crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':"./<>?abcd'
+//var crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':"./<>?abcd'
+var crazy = ' ~`!@#$%^&*(顶顅Ȓ)+=[]\\{}|;\':"./<>abcd'
/* ---------------------------------------------------------------------------
Listen for Messages
@@ -23,6 +24,8 @@ network.subscribe({
channel : "hello_world",
connect : function() {
+ console.log('connected');
+
// Publish a Message on Connect
network.publish({
channel : "hello_world",
@@ -31,6 +34,9 @@ network.subscribe({
some_key : "Hello World!",
crazy : crazy
},
+ error : function(info){
+ console.log(info);
+ },
callback : function(info){
if (!info[0]) console.log("Failed Message Delivery")
View
@@ -1086,18 +1086,27 @@ function xdr( setup ) {
, timer = timeout( function(){done(1);} , xhrtme );
data['pnsdk'] = PNSDK;
- var url = build_url(setup.url, data);
- var options = {
- hostname : setup.url[0].split("//")[1],
- port : ssl ? 443 : 80,
- path : url,
- method : 'GET'
- };
- options.agent = false;
+ var publish = setup.url[1] === 'publish';
+ var mode = publish ? 'POST' : 'GET';
+ var options = {};
+ var headers = {};
+ var payload = '';
+
+ if (publish) payload = decodeURIComponent(setup.url.pop());
+
+ var url = build_url( setup.url, data );
+
+ options.hostname = setup.url[0].split("//")[1];
+ options.port = ssl ? 443 : 80;
+ options.path = url;
+ options.method = mode;
+ options.agent = false;
+ options.body = payload;
+
require('http').globalAgent.maxSockets = Infinity;
try {
- request = (ssl ? https : http).request(options, function(response) {
+ request = (ssl ? https : http)['request'](options, function(response) {
response.setEncoding('utf8');
response.on( 'error', function(){done(1, body || { "error" : "Network Connection Error"})});
response.on( 'abort', function(){done(1, body || { "error" : "Network Connection Error"})});
@@ -1124,11 +1133,13 @@ function xdr( setup ) {
finished();
});
});
+ request.timeout = xhrtme;
request.on( 'error', function() {
done( 1, {"error":"Network Connection Error"} );
} );
+
+ if (mode == 'POST') request.write(payload+'\r\n');
request.end();
- request.timeout = xhrtme;
} catch(e) {
done(0);
@@ -105,18 +105,27 @@ function xdr( setup ) {
, timer = timeout( function(){done(1);} , xhrtme );
data['pnsdk'] = PNSDK;
- var url = build_url(setup.url, data);
- var options = {
- hostname : setup.url[0].split("//")[1],
- port : ssl ? 443 : 80,
- path : url,
- method : 'GET'
- };
- options.agent = false;
+ var publish = setup.url[1] === 'publish';
+ var mode = publish ? 'POST' : 'GET';
+ var options = {};
+ var headers = {};
+ var payload = '';
+
+ if (publish) payload = decodeURIComponent(setup.url.pop());
+
+ var url = build_url( setup.url, data );
+
+ options.hostname = setup.url[0].split("//")[1];
+ options.port = ssl ? 443 : 80;
+ options.path = url;
+ options.method = mode;
+ options.agent = false;
+ options.body = payload;
+
require('http').globalAgent.maxSockets = Infinity;
try {
- request = (ssl ? https : http).request(options, function(response) {
+ request = (ssl ? https : http)['request'](options, function(response) {
response.setEncoding('utf8');
response.on( 'error', function(){done(1, body || { "error" : "Network Connection Error"})});
response.on( 'abort', function(){done(1, body || { "error" : "Network Connection Error"})});
@@ -143,11 +152,13 @@ function xdr( setup ) {
finished();
});
});
+ request.timeout = xhrtme;
request.on( 'error', function() {
done( 1, {"error":"Network Connection Error"} );
} );
+
+ if (mode == 'POST') request.write(payload+'\r\n');
request.end();
- request.timeout = xhrtme;
} catch(e) {
done(0);

0 comments on commit 0b682da

Please sign in to comment.