55#include "core/pubnub_helper.h"
66#include "core/pubnub_timers.h"
77
8+ #if defined _WIN32
9+ #include <windows.h>
10+ #include <process.h>
11+ #else
12+ #include <pthread.h>
13+ #endif
14+
815#include <stdio.h>
916#include <time.h>
17+ #include <unistd.h>
18+
1019
11-
12- static void generate_user_id (pubnub_t * pbp )
20+ static void generate_user_id (pubnub_t * pbp )
1321{
1422 char const * user_id_default = "zeka-peka-iz-jendeka" ;
1523 struct Pubnub_UUID uuid ;
16- static struct Pubnub_UUID_String str_uuid ;
17-
24+ static struct Pubnub_UUID_String str_uuid ;
25+
1826 if (0 != pubnub_generate_uuid_v4_random (& uuid )) {
1927 pubnub_set_user_id (pbp , user_id_default );
2028 }
@@ -67,9 +75,71 @@ static int do_time(pubnub_t* pbp)
6775 return 0 ;
6876}
6977
78+ static
79+ #if defined _WIN32
80+ void
81+ #else
82+ void *
83+ #endif
84+ subscribe_thr (void * pn_sub_addr )
85+ {
86+ pubnub_t * pbp = (pubnub_t * )pn_sub_addr ;
87+ char const * chan = "hello_world" ;
88+ char const * msg ;
89+ time_t t0 ;
90+ enum pubnub_res res ;
91+
92+ puts ("Subscribing..." );
93+ time (& t0 );
94+ res = pubnub_subscribe (pbp , chan , NULL );
95+ if (PNR_STARTED == res ) {
96+ res = pubnub_await (pbp );
97+ }
98+ printf ("Subscribe/connect lasted %lf seconds.\n" , difftime (time (NULL ), t0 ));
99+ if (PNR_OK == res ) {
100+ puts ("Subscribed!" );
101+ }
102+ else {
103+ printf ("Subscribing failed with code: %d('%s')\n" ,
104+ res ,
105+ pubnub_res_2_string (res ));
106+ }
107+
108+ time (& t0 );
109+ res = pubnub_subscribe (pbp , chan , NULL );
110+ if (PNR_STARTED == res ) {
111+ res = pubnub_await (pbp );
112+ }
113+ printf ("Subscribe lasted %lf seconds.\n" , difftime (time (NULL ), t0 ));
114+ if (PNR_OK == res ) {
115+ puts ("Subscribed! Got messages:" );
116+ for (;;) {
117+ msg = pubnub_get (pbp );
118+ if (NULL == msg ) {
119+ break ;
120+ }
121+ puts (msg );
122+ }
123+ }
124+ else {
125+ printf ("Subscribing failed with code: %d('%s')\n" ,
126+ res ,
127+ pubnub_res_2_string (res ));
128+ }
129+
130+ #if !defined _WIN32
131+ return NULL ;
132+ #endif
133+ }
134+
70135
71136int main ()
72137{
138+ #if defined _WIN32
139+ uintptr_t sub_thread ;
140+ #else
141+ pthread_t sub_thread ;
142+ #endif
73143 time_t t0 ;
74144 char const * msg ;
75145 enum pubnub_res res ;
@@ -94,6 +164,19 @@ int main()
94164
95165 pubnub_set_auth (pbp , "danaske" );
96166
167+ #if defined _WIN32
168+ sub_thread = _beginthread (subscribe_thr , 0 , pbp );
169+ #else
170+ pthread_create (& sub_thread , NULL , subscribe_thr , pbp );
171+ #endif
172+ puts ("Lets go sleeping..." );
173+ sleep (4 );
174+ puts ("Done sleeping. Cancelling subscribe long-poll..." );
175+ pubnub_cancel (pbp );
176+ sleep (2 );
177+ printf ("What we have: %s\n" , pubnub_res_2_string (pubnub_last_result (pbp )));
178+ return 0 ;
179+
97180 puts ("Publishing..." );
98181 time (& t0 );
99182 res = pubnub_publish (pbp , chan , "\"Hello world from sync!\"" );
0 commit comments