@@ -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