hewei.it há 4 anos atrás
pai
commit
737e901072
2 ficheiros alterados com 13 adições e 12 exclusões
  1. 1 1
      configure
  2. 12 11
      examples/websocket_server_test.cpp

+ 1 - 1
configure

@@ -244,7 +244,7 @@ function=pthread_mutex_timedlock && header=pthread.h && check_function
 function=sem_timedwait && header=semaphore.h && check_function
 
 # Checks for options
-source config.mk
+source config.mk 2>/dev/null
 option=ENABLE_UDS && check_option
 option=USE_MULTIMAP && check_option
 

+ 12 - 11
examples/websocket_server_test.cpp

@@ -35,26 +35,27 @@ int main(int argc, char** argv) {
     }
     int port = atoi(argv[1]);
 
+    TimerID timerID = INVALID_TIMER_ID;
     WebSocketServerCallbacks ws;
-    ws.onopen = [](const WebSocketChannelPtr& channel, const std::string& url) {
+    ws.onopen = [&timerID](const WebSocketChannelPtr& channel, const std::string& url) {
         printf("onopen: GET %s\n", url.c_str());
         // send(time) every 1s
-        setInterval(1000, [channel](TimerID id) {
-            if (channel->isConnected()) {
-                char str[DATETIME_FMT_BUFLEN] = {0};
-                datetime_t dt = datetime_now();
-                datetime_fmt(&dt, str);
-                channel->send(str);
-            } else {
-                killTimer(id);
-            }
+        timerID = setInterval(1000, [channel](TimerID id) {
+            char str[DATETIME_FMT_BUFLEN] = {0};
+            datetime_t dt = datetime_now();
+            datetime_fmt(&dt, str);
+            channel->send(str);
         });
     };
     ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
         printf("onmessage: %s\n", msg.c_str());
     };
-    ws.onclose = [](const WebSocketChannelPtr& channel) {
+    ws.onclose = [&timerID](const WebSocketChannelPtr& channel) {
         printf("onclose\n");
+        if (timerID != INVALID_TIMER_ID) {
+            killTimer(timerID);
+            timerID = INVALID_TIMER_ID;
+        }
     };
 
     websocket_server_t server;