Quellcode durchsuchen

change WebSocketService::onopen

ithewei vor 3 Jahren
Ursprung
Commit
77112044cb
5 geänderte Dateien mit 40 neuen und 16 gelöschten Zeilen
  1. 17 5
      README-CN.md
  2. 17 5
      README.md
  3. 2 2
      examples/websocket_server_test.cpp
  4. 1 1
      http/server/HttpHandler.h
  5. 3 3
      http/server/WebSocketServer.h

+ 17 - 5
README-CN.md

@@ -186,6 +186,7 @@ int main() {
 
 **c++版本**: [evpp/TcpClient_test.cpp](evpp/TcpClient_test.cpp)
 ```c++
+#include <iostream>
 #include "TcpClient.h"
 using namespace hv;
 
@@ -200,7 +201,6 @@ int main() {
         std::string peeraddr = channel->peeraddr();
         if (channel->isConnected()) {
             printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
-            channel->write("hello");
         } else {
             printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
         }
@@ -210,8 +210,20 @@ int main() {
     };
     cli.start();
 
-    // press Enter to stop
-    while (getchar() != '\n');
+    std::string str;
+    while (std::getline(std::cin, str)) {
+        if (str == "close") {
+            cli.closesocket();
+        } else if (str == "start") {
+            cli.start();
+        } else if (str == "stop") {
+            cli.stop();
+            break;
+        } else {
+            if (!cli.isConnected()) break;
+            cli.send(str);
+        }
+    }
     return 0;
 }
 ```
@@ -341,8 +353,8 @@ using namespace hv;
 
 int main(int argc, char** argv) {
     WebSocketService ws;
-    ws.onopen = [](const WebSocketChannelPtr& channel, const std::string& url) {
-        printf("onopen: GET %s\n", url.c_str());
+    ws.onopen = [](const WebSocketChannelPtr& channel, const HttpRequestPtr& req) {
+        printf("onopen: GET %s\n", req->Path().c_str());
     };
     ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
         printf("onmessage: %.*s\n", (int)msg.size(), msg.data());

+ 17 - 5
README.md

@@ -149,6 +149,7 @@ int main() {
 
 **c++ version**: [evpp/TcpClient_test.cpp](evpp/TcpClient_test.cpp)
 ```c++
+#include <iostream>
 #include "TcpClient.h"
 using namespace hv;
 
@@ -163,7 +164,6 @@ int main() {
         std::string peeraddr = channel->peeraddr();
         if (channel->isConnected()) {
             printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
-            channel->write("hello");
         } else {
             printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
         }
@@ -173,8 +173,20 @@ int main() {
     };
     cli.start();
 
-    // press Enter to stop
-    while (getchar() != '\n');
+    std::string str;
+    while (std::getline(std::cin, str)) {
+        if (str == "close") {
+            cli.closesocket();
+        } else if (str == "start") {
+            cli.start();
+        } else if (str == "stop") {
+            cli.stop();
+            break;
+        } else {
+            if (!cli.isConnected()) break;
+            cli.send(str);
+        }
+    }
     return 0;
 }
 ```
@@ -304,8 +316,8 @@ using namespace hv;
 
 int main(int argc, char** argv) {
     WebSocketService ws;
-    ws.onopen = [](const WebSocketChannelPtr& channel, const std::string& url) {
-        printf("onopen: GET %s\n", url.c_str());
+    ws.onopen = [](const WebSocketChannelPtr& channel, const HttpRequestPtr& req) {
+        printf("onopen: GET %s\n", req->Path().c_str());
     };
     ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
         printf("onmessage: %.*s\n", (int)msg.size(), msg.data());

+ 2 - 2
examples/websocket_server_test.cpp

@@ -58,8 +58,8 @@ int main(int argc, char** argv) {
     });
 
     WebSocketService ws;
-    ws.onopen = [](const WebSocketChannelPtr& channel, const std::string& url) {
-        printf("onopen: GET %s\n", url.c_str());
+    ws.onopen = [](const WebSocketChannelPtr& channel, const HttpRequestPtr& req) {
+        printf("onopen: GET %s\n", req->Path().c_str());
         MyContext* ctx = channel->newContext<MyContext>();
         // send(time) every 1s
         ctx->timerID = setInterval(1000, [channel](TimerID id) {

+ 1 - 1
http/server/HttpHandler.h

@@ -79,7 +79,7 @@ public:
     void WebSocketOnOpen() {
         ws_channel->status = hv::SocketChannel::CONNECTED;
         if (ws_service && ws_service->onopen) {
-            ws_service->onopen(ws_channel, req->url);
+            ws_service->onopen(ws_channel, req);
         }
     }
     void WebSocketOnClose() {

+ 3 - 3
http/server/WebSocketServer.h

@@ -15,9 +15,9 @@
 namespace hv {
 
 struct WebSocketService {
-    std::function<void(const WebSocketChannelPtr&, const std::string&)> onopen;
-    std::function<void(const WebSocketChannelPtr&, const std::string&)> onmessage;
-    std::function<void(const WebSocketChannelPtr&)>                     onclose;
+    std::function<void(const WebSocketChannelPtr&, const HttpRequestPtr&)>  onopen;
+    std::function<void(const WebSocketChannelPtr&, const std::string&)>     onmessage;
+    std::function<void(const WebSocketChannelPtr&)>                         onclose;
     int ping_interval;
 
     WebSocketService() {