Ver código fonte

Update README

ithewei 3 anos atrás
pai
commit
f07bb9ff17
3 arquivos alterados com 157 adições e 138 exclusões
  1. 5 0
      BUILD.md
  2. 76 69
      README-CN.md
  3. 76 69
      README.md

+ 5 - 0
BUILD.md

@@ -154,3 +154,8 @@ make clean && make
 ./configure --with-mqtt
 make clean && make
 ```
+
+### More
+```
+./configure --help
+```

+ 76 - 69
README-CN.md

@@ -137,36 +137,6 @@ bin/wrk -c 1000 -d 10 -t 4 http://127.0.0.1:8080/
 ### TCP
 #### TCP服务端
 **c版本**: [examples/tcp_echo_server.c](examples/tcp_echo_server.c)
-```c
-#include "hloop.h"
-
-static void on_close(hio_t* io) {
-    printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
-}
-
-static void on_recv(hio_t* io, void* buf, int readbytes) {
-    // echo
-    hio_write(io, buf, readbytes);
-}
-
-static void on_accept(hio_t* io) {
-    hio_setcb_close(io, on_close);
-    hio_setcb_read(io, on_recv);
-    hio_read(io);
-}
-
-int main() {
-    int port = 1234;
-    hloop_t* loop = hloop_new(0);
-    hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
-    if (listenio == NULL) {
-        return -1;
-    }
-    hloop_run(loop);
-    hloop_free(&loop);
-    return 0;
-}
-```
 
 **c++版本**: [evpp/TcpServer_test.cpp](evpp/TcpServer_test.cpp)
 ```c++
@@ -212,41 +182,6 @@ int main() {
 
 #### TCP客户端
 **c版本**: [examples/nc.c](examples/nc.c)
-```c
-#include "hloop.h"
-
-static void on_close(hio_t* io) {
-    printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
-}
-
-static void on_recv(hio_t* io, void* buf, int readbytes) {
-    printf("< %.*s\n", readbytes, (char*)buf);
-}
-
-static void on_connect(hio_t* io) {
-    hio_setcb_read(io, on_recv);
-    hio_read(io);
-
-    hio_write(io, "hello", 5);
-}
-
-int main() {
-    const char host[] = "127.0.0.1";
-    int port = 1234;
-    hloop_t* loop = hloop_new(0);
-    hio_t* io = hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
-    if (io == NULL) {
-        perror("socket");
-        exit(1);
-    }
-    hio_setcb_connect(io, on_connect);
-    hio_setcb_close(io, on_close);
-    hio_connect(io);
-    hloop_run(loop);
-    hloop_free(&loop);
-    return 0;
-}
-```
 
 **c++版本**: [evpp/TcpClient_test.cpp](evpp/TcpClient_test.cpp)
 ```c++
@@ -287,6 +222,7 @@ int main() {
 **golang gin 风格**
 ```c++
 #include "HttpServer.h"
+using namespace hv;
 
 int main() {
     HttpService router;
@@ -315,10 +251,11 @@ int main() {
         return ctx->send(ctx->body(), ctx->type());
     });
 
-    http_server_t server;
-    server.port = 8080;
-    server.service = &router;
-    http_server_run(&server);
+    HttpServer server;
+    server.registerHttpService(&router);
+    server.setPort(8080);
+    server.setThreadNum(4);
+    server.run();
     return 0;
 }
 ```
@@ -394,6 +331,76 @@ int main() {
 }
 ```
 
+### WebSocket
+#### WebSocket服务端
+见[examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
+```c++
+#include "WebSocketServer.h"
+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.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
+        printf("onmessage: %s\n", msg.c_str());
+    };
+    ws.onclose = [](const WebSocketChannelPtr& channel) {
+        printf("onclose\n");
+    };
+
+    WebSocketServer server;
+    server.registerWebSocketService(&ws);
+    server.setPort(9999);
+    server.setThreadNum(4);
+    server.run();
+    return 0;
+}
+```
+
+#### WebSocket客户端
+见[examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
+```c++
+#include "WebSocketClient.h"
+using namespace hv;
+
+int main(int argc, char** argv) {
+    WebSocketClient ws;
+    ws.onopen = []() {
+        printf("onopen\n");
+    };
+    ws.onclose = []() {
+        printf("onclose\n");
+    };
+    ws.onmessage = [](const std::string& msg) {
+        printf("onmessage: %s\n", msg.c_str());
+    };
+
+    // reconnect: 1,2,4,8,10,10,10...
+    reconn_setting_t reconn;
+    reconn_setting_init(&reconn);
+    reconn.min_delay = 1000;
+    reconn.max_delay = 10000;
+    reconn.delay_policy = 2;
+    ws.setReconnect(&reconn);
+
+    ws.open("ws://127.0.0.1:9999/test");
+
+    std::string str;
+    while (std::getline(std::cin, str)) {
+        if (!ws.isConnected()) break;
+        if (str == "quit") {
+            ws.close();
+            break;
+        }
+        ws.send(str);
+    }
+
+    return 0;
+}
+```
+
 ## 🍭 更多示例
 
 ### c版本

+ 76 - 69
README.md

@@ -108,36 +108,6 @@ bin/wrk -c 1000 -d 10 -t 4 http://127.0.0.1:8080/
 ### TCP
 #### tcp server
 **c version**: [examples/tcp_echo_server.c](examples/tcp_echo_server.c)
-```c
-#include "hloop.h"
-
-static void on_close(hio_t* io) {
-    printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
-}
-
-static void on_recv(hio_t* io, void* buf, int readbytes) {
-    // echo
-    hio_write(io, buf, readbytes);
-}
-
-static void on_accept(hio_t* io) {
-    hio_setcb_close(io, on_close);
-    hio_setcb_read(io, on_recv);
-    hio_read(io);
-}
-
-int main() {
-    int port = 1234;
-    hloop_t* loop = hloop_new(0);
-    hio_t* listenio = hloop_create_tcp_server(loop, "0.0.0.0", port, on_accept);
-    if (listenio == NULL) {
-        return -1;
-    }
-    hloop_run(loop);
-    hloop_free(&loop);
-    return 0;
-}
-```
 
 **c++ version**: [evpp/TcpServer_test.cpp](evpp/TcpServer_test.cpp)
 ```c++
@@ -175,41 +145,6 @@ int main() {
 
 #### tcp client
 **c version**: [examples/nc.c](examples/nc.c)
-```c
-#include "hloop.h"
-
-static void on_close(hio_t* io) {
-    printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
-}
-
-static void on_recv(hio_t* io, void* buf, int readbytes) {
-    printf("< %.*s\n", readbytes, (char*)buf);
-}
-
-static void on_connect(hio_t* io) {
-    hio_setcb_read(io, on_recv);
-    hio_read(io);
-
-    hio_write(io, "hello", 5);
-}
-
-int main() {
-    const char host[] = "127.0.0.1";
-    int port = 1234;
-    hloop_t* loop = hloop_new(0);
-    hio_t* io = hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
-    if (io == NULL) {
-        perror("socket");
-        exit(1);
-    }
-    hio_setcb_connect(io, on_connect);
-    hio_setcb_close(io, on_close);
-    hio_connect(io);
-    hloop_run(loop);
-    hloop_free(&loop);
-    return 0;
-}
-```
 
 **c++ version**: [evpp/TcpClient_test.cpp](evpp/TcpClient_test.cpp)
 ```c++
@@ -250,6 +185,7 @@ see [examples/http_server_test.cpp](examples/http_server_test.cpp)
 **golang gin style**
 ```c++
 #include "HttpServer.h"
+using namespace hv;
 
 int main() {
     HttpService router;
@@ -278,10 +214,11 @@ int main() {
         return ctx->send(ctx->body(), ctx->type());
     });
 
-    http_server_t server;
-    server.port = 8080;
-    server.service = &router;
-    http_server_run(&server);
+    HttpServer server;
+    server.registerHttpService(&router);
+    server.setPort(8080);
+    server.setThreadNum(4);
+    server.run();
     return 0;
 }
 ```
@@ -357,6 +294,76 @@ int main() {
 }
 ```
 
+### WebSocket
+#### WebSocket server
+see [examples/websocket_server_test.cpp](examples/websocket_server_test.cpp)
+```c++
+#include "WebSocketServer.h"
+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.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
+        printf("onmessage: %s\n", msg.c_str());
+    };
+    ws.onclose = [](const WebSocketChannelPtr& channel) {
+        printf("onclose\n");
+    };
+
+    WebSocketServer server;
+    server.registerWebSocketService(&ws);
+    server.setPort(9999);
+    server.setThreadNum(4);
+    server.run();
+    return 0;
+}
+```
+
+#### WebSocket client
+see [examples/websocket_client_test.cpp](examples/websocket_client_test.cpp)
+```c++
+#include "WebSocketClient.h"
+using namespace hv;
+
+int main(int argc, char** argv) {
+    WebSocketClient ws;
+    ws.onopen = []() {
+        printf("onopen\n");
+    };
+    ws.onclose = []() {
+        printf("onclose\n");
+    };
+    ws.onmessage = [](const std::string& msg) {
+        printf("onmessage: %s\n", msg.c_str());
+    };
+
+    // reconnect: 1,2,4,8,10,10,10...
+    reconn_setting_t reconn;
+    reconn_setting_init(&reconn);
+    reconn.min_delay = 1000;
+    reconn.max_delay = 10000;
+    reconn.delay_policy = 2;
+    ws.setReconnect(&reconn);
+
+    ws.open("ws://127.0.0.1:9999/test");
+
+    std::string str;
+    while (std::getline(std::cin, str)) {
+        if (!ws.isConnected()) break;
+        if (str == "quit") {
+            ws.close();
+            break;
+        }
+        ws.send(str);
+    }
+
+    return 0;
+}
+```
+
 ## 🍭 More examples
 ### c version
 - [examples/hloop_test.c](examples/hloop_test.c)