Skip to content

Commit da28a48

Browse files
Merge branch 'master' into 0.9.2
2 parents ffa4a49 + ef1d7d6 commit da28a48

File tree

3 files changed

+55
-19
lines changed

3 files changed

+55
-19
lines changed

libs/ofxLibwebsockets/include/ofxLibwebsockets/Client.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace ofxLibwebsockets {
1919
string channel;
2020
string protocol;
2121
int version;
22+
bool reconnect;
23+
int reconnectInterval;
2224

2325
// advanced: timeout options
2426
// names are from libwebsockets (ka == keep alive)
@@ -36,7 +38,7 @@ namespace ofxLibwebsockets {
3638

3739
Client();
3840
~Client();
39-
41+
4042
// Note: the boolean returned here == libwebsockets setup success
4143
// You will receive an "onOpen" event on successful connect
4244
// and "onClose" on unsuccessful
@@ -81,7 +83,11 @@ namespace ofxLibwebsockets {
8183
Connection * getConnection(){
8284
return connection;
8385
}
84-
86+
87+
// Note: you do not need to call this function! It is called
88+
// automatically and only used for reconnecting
89+
void update(ofEventArgs& args);
90+
8591
protected:
8692
ClientOptions defaultOptions;
8793
void onClose( Event& args );
@@ -95,6 +101,8 @@ namespace ofxLibwebsockets {
95101

96102
//wrap protocol
97103
Protocol clientProtocol;
98-
104+
105+
bool bShouldReconnect;
106+
uint64_t lastReconnectTime;
99107
};
100108
};

libs/ofxLibwebsockets/src/Client.cpp

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ){

libs/ofxLibwebsockets/src/Server.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,12 @@ namespace ofxLibwebsockets {
234234
//unlock();
235235
}
236236
}
237-
lock();
238-
libwebsocket_service(context, waitMillis);
239-
unlock();
237+
238+
if (lock())
239+
{
240+
libwebsocket_service(context, waitMillis);
241+
unlock();
242+
}
240243
}
241244
}
242245
}

0 commit comments

Comments
 (0)