Skip to content

Commit c0c842a

Browse files
committed
Lower default Socket::wait timeouts, updated sample
1 parent 0307b5f commit c0c842a

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

include/asl/Socket.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,13 @@ class ASL_API Socket : public SmartObject
241241
Waits until there is incoming data in the socket or it is disconnected for a maximum time, and
242242
returns true if some of that happened before timeout
243243
*/
244-
bool waitInput(double timeout = 60) { return _()->waitInput(timeout); }
244+
bool waitInput(double timeout = 2) { return _()->waitInput(timeout); }
245245

246246
/**
247247
Waits until there is incoming data in the socket or it is disconnected for a maximum time, and
248248
returns true only if there is data to read (false may mean no data or disconnection)
249249
*/
250-
bool waitData(double timeout = 60) { return waitInput(timeout) && !disconnected(); }
250+
bool waitData(double timeout = 2) { return waitInput(timeout) && !disconnected(); }
251251
/**
252252
Returns true if there was some communication error in this socket
253253
*/

samples/http-websocket/server.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ void Server::serve(HttpRequest& request, HttpResponse& response)
7878
{
7979
Var data = request.data();
8080
printf("HTTP %s\n", *data.string());
81-
if (data.has("r")) _r = data["r"];
82-
if (data.has("v")) _v = data["v"];
81+
data.read("r", _r);
82+
data.read("v", _v);
8383
response.put(Var("status", "OK"));
8484
return;
8585
}
@@ -101,24 +101,17 @@ class MyWebSocketServer : public WebSocketServer
101101
void serve(WebSocket& ws)
102102
{
103103
printf("New client: %i\n", clients().length());
104-
while (1) {
105-
if( ws.wait() )
104+
while (!ws.closed() && !_requestStop)
105+
{
106+
if (!ws.waitData())
107+
continue;
108+
Var msg = ws.receive(); // receive message and extract radius and speed
109+
if (msg.ok())
106110
{
107-
if (ws.closed())
108-
break;
109-
Var msg = ws.receive(); // receive message and extract radius and speed
110-
if (msg.ok())
111-
{
112-
printf("WS %s\n", *msg.string());
113-
if (msg.has("r"))
114-
Server::_r = msg["r"];
115-
if (msg.has("v"))
116-
Server::_v = msg["v"];
117-
ws.send(Var("status", "ok"));
118-
}
119-
}
120-
if (ws.closed()) {
121-
break;
111+
printf("WS %s\n", *msg.string());
112+
msg.read("r", Server::_r);
113+
msg.read("v", Server::_v);
114+
ws.send(Var("status", "ok"));
122115
}
123116
}
124117
printf("Client out: %i (code %i)\n", clients().length()-1, ws.code());

0 commit comments

Comments
 (0)