@@ -254,6 +254,7 @@ class PubNub {
254
254
d_uuid = 0 ;
255
255
d_auth = 0 ;
256
256
d_last_http_status_code_class = http_scc_unknown;
257
+ set_port (http_port);
257
258
}
258
259
259
260
/* *
@@ -281,6 +282,16 @@ class PubNub {
281
282
http_scc_server_error = 5
282
283
};
283
284
285
+ /* *
286
+ * Possible TCP/IP ports to use when connecting to Pubnub.
287
+ */
288
+ enum port_for_connection {
289
+ /* * Connect via HTTP on its default port */
290
+ http_port,
291
+ /* * Connect via TLS (formerly known as SSL) on its default port) */
292
+ tls_port
293
+ };
294
+
284
295
/* *
285
296
* Set the UUID identification of PubNub client. This is useful
286
297
* e.g. for presence identification.
@@ -299,6 +310,23 @@ class PubNub {
299
310
* in begin()). */
300
311
void set_auth (const char * auth) { d_auth = auth; }
301
312
313
+ /* *
314
+ * Set the TCP/IP port to use when connecting to Pubnub.
315
+ * Basically, only call this if your client supports SSL/TLS
316
+ * as you will need to set the `tls_port`.
317
+ */
318
+ void set_port (port_for_connection port) {
319
+ switch (port) {
320
+ case http_port:
321
+ default :
322
+ d_port = 80 ;
323
+ break ;
324
+ case tls_port:
325
+ d_port = 443 ;
326
+ break ;
327
+ }
328
+ }
329
+
302
330
/* *
303
331
* Publish/Send a message (assumed to be well-formed JSON) to a
304
332
* given channel.
@@ -397,6 +425,9 @@ class PubNub {
397
425
const char * d_uuid;
398
426
const char * d_auth;
399
427
428
+ // / TCP/IP port to use.
429
+ unsigned d_port;
430
+
400
431
// / The HTTP status code class of the last PubNub transaction
401
432
http_status_code_class d_last_http_status_code_class;
402
433
@@ -575,14 +606,12 @@ inline PubNonSubClient* PubNub::publish(const char* channel,
575
606
int timeout)
576
607
{
577
608
PubNonSubClient& client = publish_client;
578
- unsigned long t_start;
579
609
int have_param = 0 ;
610
+ unsigned long t_start = millis ();
580
611
581
- retry:
582
- t_start = millis ();
583
612
/* connect() timeout is about 30s, much lower than our usual
584
613
* timeout is. */
585
- int rslt = client.connect (d_origin, 80 );
614
+ int rslt = client.connect (d_origin, d_port );
586
615
if (rslt != 1 ) {
587
616
DBGprint (" Connection error " );
588
617
DBGprintln (rslt);
@@ -638,30 +667,30 @@ inline PubNonSubClient* PubNub::publish(const char* channel,
638
667
case PubNub_BH_OK:
639
668
return &client;
640
669
case PubNub_BH_ERROR:
670
+ DBGprintln (" publish() BH_ERROR" );
641
671
client.stop ();
642
672
while (client.connected ())
643
673
;
644
674
return 0 ;
645
675
case PubNub_BH_TIMEOUT:
676
+ DBGprintln (" publish() BH_TIMEOUT" );
646
677
client.stop ();
647
678
while (client.connected ())
648
679
;
649
- goto retry ;
680
+ return 0 ;
650
681
}
651
682
}
652
683
653
684
654
685
inline PubSubClient* PubNub::subscribe (const char * channel, int timeout)
655
686
{
656
687
PubSubClient& client = subscribe_client;
657
- unsigned long t_start;
658
688
int have_param = 0 ;
689
+ unsigned long t_start = millis ();
659
690
660
- retry:
661
- t_start = millis ();
662
691
/* connect() timeout is about 30s, much lower than our usual
663
692
* timeout is. */
664
- if (!client.connect (d_origin, 80 )) {
693
+ if (!client.connect (d_origin, d_port )) {
665
694
DBGprintln (" Connection error" );
666
695
client.stop ();
667
696
return 0 ;
@@ -716,28 +745,30 @@ inline PubSubClient* PubNub::subscribe(const char* channel, int timeout)
716
745
return &client;
717
746
718
747
case PubNub_BH_ERROR:
748
+ DBGprintln (" subscribe() BH_ERROR" );
719
749
client.stop ();
720
750
while (client.connected ())
721
751
;
722
752
return 0 ;
723
753
724
754
case PubNub_BH_TIMEOUT:
755
+ DBGprintln (" subscribe() BH_TIMEOUT" );
725
756
client.stop ();
757
+ DBGprintln (" subscribe() BH_TIMEOUT stopped" );
726
758
while (client.connected ())
727
759
;
728
- goto retry;
760
+ DBGprintln (" subscribe() BH_TIMEOUT disconnected" );
761
+ return 0 ;
729
762
}
730
763
}
731
764
732
765
733
766
inline PubNonSubClient* PubNub::history (const char * channel, int limit, int timeout)
734
767
{
735
768
PubNonSubClient& client = history_client;
736
- unsigned long t_start;
769
+ unsigned long t_start = millis () ;
737
770
738
- retry:
739
- t_start = millis ();
740
- if (!client.connect (d_origin, 80 )) {
771
+ if (!client.connect (d_origin, d_port)) {
741
772
DBGprintln (" Connection error" );
742
773
client.stop ();
743
774
return 0 ;
@@ -757,15 +788,17 @@ inline PubNonSubClient* PubNub::history(const char* channel, int limit, int time
757
788
case PubNub_BH_OK:
758
789
return &client;
759
790
case PubNub_BH_ERROR:
791
+ DBGprintln (" history() BH_ERROR" );
760
792
client.stop ();
761
793
while (client.connected ())
762
794
;
763
795
return 0 ;
764
796
case PubNub_BH_TIMEOUT:
797
+ DBGprintln (" history() BH_TIMEOUT" );
765
798
client.stop ();
766
799
while (client.connected ())
767
800
;
768
- goto retry ;
801
+ return 0 ;
769
802
}
770
803
}
771
804
0 commit comments