@@ -2,7 +2,7 @@ var clOptions = [];
22var config = { } ;
33
44process . argv . forEach ( function ( val , index , array ) {
5- // console.log(index + ': ' + val);
5+ console . log ( index + ': ' + val ) ;
66 clOptions [ index ] = val ;
77} ) ;
88
@@ -12,43 +12,27 @@ var keysets = {
1212 "pub" : "pub-c-fb5fa283-0d93-424f-bf86-d9aca2366c86" ,
1313 "sub" : "sub-c-d247d250-9dbd-11e3-8008-02ee2ddab7fe" ,
1414 "sec" : "sec-c-MmI2YjRjODAtNWU5My00ZmZjLTg0MzUtZGM1NGExNjJkNjg1" ,
15- "description" : "499 on Leave Enabled "
15+ "description" : "Compatibility Mode ON "
1616 } ,
1717
1818 "keyset2" : {
1919 "pub" : "pub-c-c9b0fe21-4ae1-433b-b766-62667cee65ef" ,
2020 "sub" : "sub-c-d91ee366-9dbd-11e3-a759-02ee2ddab7fe" ,
2121 "sec" : "sec-c-ZDUxZGEyNmItZjY4Ny00MjJmLWE0MjQtZTQyMDM0NTY2MDVk" ,
22- "description" : "499 on Leave Disabled "
22+ "description" : "Compatibility Mode OFF "
2323 }
2424} ;
2525
26+ var pubnub = { } ;
2627
2728// console.log(clOptions);
28-
2929//[ 0 'node',
3030// 1 '/Users/gcohen/clients/javascript/node.js/examples/ptest.js',
3131// 2 'keyset1',
3232// 3 'beta',
3333// 4 'gecA',
3434// 5 'gecB' ]
3535
36- validateArgs ( clOptions ) ;
37-
38- function usageOutput ( ) {
39- console . log ( "\nUsage: " + clOptions [ 1 ] + " KEYSET ENVIRONMENT CHANNEL(S)" ) ;
40- console . log ( "KEYSET: 1 or 2" ) ;
41- console . log ( "ENVIRONMENT: prod or beta" ) ;
42- console . log ( "CHANNEL(S): CH1[,CH2,CH3,CH4]" ) ;
43- console . log ( "\n" ) ;
44- process . exit ( 1 ) ;
45- }
46-
47- function initClientWithArgs ( config ) {
48- console . log ( "Using keyset " + config . keyset . description + "." ) ;
49- console . log ( "Using environment " + config . environment + "." ) ;
50-
51- }
5236
5337function validateArgs ( opts ) {
5438
@@ -69,49 +53,142 @@ function validateArgs(opts) {
6953
7054 // set env
7155 if ( ( clOptions [ 3 ] == "beta" ) || ( clOptions [ 3 ] == "prod" ) ) {
72- config . environment = clOptions [ 3 ] ;
56+ if ( clOptions [ 3 ] == "beta" ) {
57+ config . origin = "presence-beta.pubnub.com" ;
58+ } else {
59+ config . origin = "pubsub.pubnub.com" ;
60+ }
61+
7362 } else {
7463 usageOutput ( ) ;
7564 }
7665
7766 // set channels
78- if ( ( clOptions [ 3 ] == "beta" ) || ( clOptions [ 3 ] == "prod" ) ) {
79- config . environment = clOptions [ 3 ] ;
67+ if ( clOptions [ 4 ] ) {
68+ config . ch1 = clOptions [ 4 ] ;
69+ }
70+
71+ if ( clOptions [ 5 ] ) {
72+ config . ch2 = clOptions [ 5 ] ;
73+ }
74+
75+ // set uuid
76+ if ( clOptions [ 6 ] ) {
77+ config . uuid = clOptions [ 6 ] ;
8078 } else {
81- usageOutput ( ) ;
79+ config . uuid = Math . random ( ) ;
80+ }
81+
82+ config . ssl = false ;
83+
84+ if ( clOptions [ 7 ] == 0 ) {
85+ config . ssl = false ;
86+ } else if ( clOptions [ 7 ] == 1 ) {
87+ config . ssl = true ;
8288 }
8389
90+
8491 initClientWithArgs ( config ) ;
8592
93+ }
94+
95+ validateArgs ( clOptions ) ;
8696
97+ function usageOutput ( ) {
98+ console . log ( "\nUsage: " + clOptions [ 1 ] + " KEYSET ENVIRONMENT CHANNEL(S)" ) ;
99+ console . log ( "KEYSET: 1 or 2" ) ;
100+ console . log ( "ENVIRONMENT: prod or beta" ) ;
101+ console . log ( "CH1" ) ;
102+ console . log ( "CH2" ) ;
103+ console . log ( "UUID" ) ;
104+ console . log ( "SSL" ) ;
105+ console . log ( "\n" ) ;
106+ console . log ( "Example Usage: node ptest.js 1 beta gecA gecB myUUIDHere 0\n" )
107+ process . exit ( 1 ) ;
87108}
88109
110+ function connected ( ) {
111+ console . log ( "Connected." ) ;
112+ }
89113
90- var pubnub = require ( "./../pubnub.js" ) . init ( {
91- publish_key : "demo" ,
92- subscribe_key : "demo"
114+ function initClientWithArgs ( config ) {
115+ console . log ( "Using keyset " + config . keyset . description + " with keys " + config . keyset . sub + " " + config . keyset . pub ) ;
116+ console . log ( "Using environment " + config . environment + "." ) ;
117+ console . log ( "Setting ch1 to " + config . ch1 ) ;
118+ console . log ( "Setting ch2 to " + config . ch2 ) ;
119+ console . log ( "Setting UUID to " + config . uuid ) ;
120+ console . log ( "Setting SSL to " + config . ssl ) ;
121+
122+
123+ pubnub = require ( "./../pubnub.js" ) . init ( {
124+ origin : config . origin ,
125+ publish_key : config . keyset . pub ,
126+ subscribe_key : config . keyset . sub ,
127+ uuid : config . uuid
93128 } )
94- , exec = require ( 'child_process' ) . exec ;
95-
96- pubnub . subscribe ( {
97- channel : "my_channel" ,
98- connect : function ( ) {
99- // Publish a Message on Connect
100- pubnub . publish ( {
101- channel : "my_channel" ,
102- message : { text : 'Ready to Receive Voice Script.' }
103- } ) ;
104- } ,
105- callback : function ( message ) {
106- console . log ( message ) ;
107- exec ( 'say ' + (
108- 'voice' in message &&
109- message . voice ? '-v ' +
110- message . voice + ' ' : ''
111- ) + message . text ) ;
112129
113- } ,
114- error : function ( ) {
115- console . log ( "Network Connection Dropped" ) ;
130+
131+
132+
133+
134+
135+
136+
137+ , exec = require ( 'child_process' ) . exec ;
138+ }
139+
140+
141+ var readline = require ( 'readline' ) ,
142+ rl = readline . createInterface ( process . stdin , process . stdout ) ;
143+
144+ rl . setPrompt ( '> ' ) ;
145+ rl . prompt ( ) ;
146+
147+ rl . on ( 'line' , function ( line ) {
148+ switch ( line . trim ( ) ) {
149+ case 'suba' :
150+ console . log ( 'Subscribing to ' + config . ch1 ) ;
151+ subscribe ( config . ch1 ) ;
152+ break ;
153+ case 'subb' :
154+ console . log ( 'Subscribing to ' + config . ch2 ) ;
155+ subscribe ( config . ch2 ) ;
156+ break ;
157+ case 'unsuba' :
158+ console . log ( 'UnSubscribing to ' + config . ch1 ) ;
159+ unsubscribe ( config . ch1 ) ;
160+ break ;
161+ default :
162+ console . log ( 'ERROR ERROR ERROR `' + line . trim ( ) + '`' ) ;
163+ break ;
116164 }
117- } ) ;
165+ rl . prompt ( ) ;
166+ } ) . on ( 'close' , function ( ) {
167+ console . log ( 'BYE BYE!' ) ;
168+ process . exit ( 0 ) ;
169+ } ) ;
170+
171+
172+ function subscribe ( ch ) {
173+ pubnub . subscribe ( {
174+ channel : ch ,
175+ connect : function ( ) {
176+ console . log ( "Connected to " + ch + "." ) ;
177+ } ,
178+
179+ callback : function ( message ) {
180+ console . log ( message ) ;
181+
182+ } ,
183+ error : function ( ) {
184+ console . log ( "Error." ) ;
185+ }
186+ } ) ;
187+ }
188+
189+ function unsubscribe ( ch ) {
190+ pubnub . unsubscribe ( {
191+ channel : ch
192+ } ) ;
193+
194+ }
0 commit comments