@@ -12,12 +12,14 @@ namespace ofxLibwebsockets {
1212
1313 ClientOptions defaultClientOptions (){
1414 ClientOptions opts;
15- opts.host = " localhost" ;
16- opts.port = 80 ;
17- opts.bUseSSL = false ;
18- opts.channel = " /" ;
19- opts.protocol = " NULL" ;
20- opts.version = -1 ; // use latest version
15+ opts.host = " localhost" ;
16+ opts.port = 80 ;
17+ opts.bUseSSL = false ;
18+ opts.channel = " /" ;
19+ opts.protocol = " NULL" ;
20+ opts.version = -1 ; // use latest version
21+ opts.reconnect = true ;
22+ opts.reconnectInterval = 1000 ;
2123
2224 opts.ka_time = 0 ;
2325 opts.ka_probes = 0 ;
@@ -33,14 +35,16 @@ namespace ofxLibwebsockets {
3335 reactors.push_back (this );
3436
3537 defaultOptions = defaultClientOptions ();
36-
38+
39+ ofAddListener ( ofEvents ().update , this , &Client::update);
3740 ofAddListener ( clientProtocol.oncloseEvent , this , &Client::onClose);
3841 }
3942
4043
4144 // --------------------------------------------------------------
4245 Client::~Client (){
4346 exit ();
47+ ofRemoveListener ( ofEvents ().update , this , &Client::update);
4448 }
4549
4650 // --------------------------------------------------------------
@@ -67,7 +71,9 @@ namespace ofxLibwebsockets {
6771 address = options.host ;
6872 port = options.port ;
6973 channel = options.channel ;
70-
74+ defaultOptions = options;
75+ bShouldReconnect = defaultOptions.reconnect ;
76+
7177 /*
7278 enum lws_log_levels {
7379 LLL_ERR = 1 << 0,
@@ -172,6 +178,9 @@ namespace ofxLibwebsockets {
172178
173179 // --------------------------------------------------------------
174180 void Client::close (){
181+ // Self-initiated call to close() means we shouldn't try to reconnect
182+ bShouldReconnect = false ;
183+
175184 if (isThreadRunning ()){
176185 waitForThread (true );
177186 } else {
@@ -200,7 +209,9 @@ namespace ofxLibwebsockets {
200209 if ( context != NULL ){
201210 closeAndFree = true ;
202211 lwsconnection = NULL ;
203- }
212+ }
213+
214+ lastReconnectTime = ofGetElapsedTimeMillis ();
204215 }
205216
206217 // --------------------------------------------------------------
@@ -230,7 +241,18 @@ namespace ofxLibwebsockets {
230241 connection->sendBinary (data,size);
231242 }
232243 }
233-
244+
245+ // --------------------------------------------------------------
246+ void Client::update (ofEventArgs& args) {
247+ if (!isConnected () && bShouldReconnect) {
248+ uint64_t now = ofGetElapsedTimeMillis ();
249+ if (now - lastReconnectTime > defaultOptions.reconnectInterval ) {
250+ lastReconnectTime = now;
251+ connect ( defaultOptions );
252+ }
253+ }
254+ }
255+
234256 // --------------------------------------------------------------
235257 void Client::threadedFunction (){
236258 while ( isThreadRunning () ){
@@ -244,9 +266,12 @@ namespace ofxLibwebsockets {
244266 if (context != NULL && lwsconnection != NULL ){
245267 // libwebsocket_callback_on_writable(context,lwsconnection);
246268 connection->update ();
247- lock ();
248- int n = libwebsocket_service (context, waitMillis);
249- unlock ();
269+
270+ if (lock ())
271+ {
272+ int n = libwebsocket_service (context, waitMillis);
273+ unlock ();
274+ }
250275 } else {
251276 stopThread ();
252277 if ( context != NULL ){
0 commit comments