ithewei 4 years ago
parent
commit
8444528c0f
2 changed files with 14 additions and 7 deletions
  1. 3 6
      examples/http_server_test.cpp
  2. 11 1
      examples/websocket_server_test.cpp

+ 3 - 6
examples/http_server_test.cpp

@@ -78,13 +78,10 @@ int main(int argc, char** argv) {
     // uncomment to test multi-threads
     // server.worker_threads = 4;
 
-#if 1
-    http_server_run(&server);
-#else
-    // test http_server_stop
     http_server_run(&server, 0);
-    hv_sleep(10);
+
+    // press Enter to stop
+    while (getchar() != '\n');
     http_server_stop(&server);
-#endif
     return 0;
 }

+ 11 - 1
examples/websocket_server_test.cpp

@@ -51,6 +51,11 @@ int main(int argc, char** argv) {
     }
     int port = atoi(argv[1]);
 
+    HttpService http;
+    http.GET("/ping", [](const HttpContextPtr& ctx) {
+        return ctx->send("pong");
+    });
+
     WebSocketService ws;
     ws.onopen = [](const WebSocketChannelPtr& channel, const std::string& url) {
         printf("onopen: GET %s\n", url.c_str());
@@ -90,7 +95,12 @@ int main(int argc, char** argv) {
         return -20;
     }
 #endif
+    server.service = &http;
     server.ws = &ws;
-    websocket_server_run(&server);
+    websocket_server_run(&server, 0);
+
+    // press Enter to stop
+    while (getchar() != '\n');
+    websocket_server_stop(&server);
     return 0;
 }