Procházet zdrojové kódy

#83 WebSocketService::ping_interval

ithewei před 4 roky
rodič
revize
053529c07a

+ 1 - 1
examples/websocket_server_test.cpp

@@ -51,7 +51,7 @@ int main(int argc, char** argv) {
     }
     int port = atoi(argv[1]);
 
-    WebSocketServerCallbacks ws;
+    WebSocketService ws;
     ws.onopen = [](const WebSocketChannelPtr& channel, const std::string& url) {
         printf("onopen: GET %s\n", url.c_str());
         MyContext* ctx = channel->newContext<MyContext>();

+ 8 - 8
http/server/HttpHandler.h

@@ -85,7 +85,7 @@ public:
 
     // for websocket
     WebSocketHandlerPtr         ws;
-    WebSocketServerCallbacks*   ws_cbs;
+    WebSocketService*           ws_service;
 
     HttpHandler() {
         protocol = UNKNOWN;
@@ -93,7 +93,7 @@ public:
         ssl = false;
         service = NULL;
         files = NULL;
-        ws_cbs = NULL;
+        ws_service = NULL;
     }
 
     bool Init(int http_version = 1) {
@@ -149,19 +149,19 @@ public:
     }
     void WebSocketOnOpen() {
         ws->onopen();
-        if (ws_cbs && ws_cbs->onopen) {
-            ws_cbs->onopen(ws->channel, req->url);
+        if (ws_service && ws_service->onopen) {
+            ws_service->onopen(ws->channel, req->url);
         }
     }
     void WebSocketOnClose() {
         ws->onclose();
-        if (ws_cbs && ws_cbs->onclose) {
-            ws_cbs->onclose(ws->channel);
+        if (ws_service && ws_service->onclose) {
+            ws_service->onclose(ws->channel);
         }
     }
     void WebSocketOnMessage(const std::string& msg) {
-        if (ws_cbs && ws_cbs->onmessage) {
-            ws_cbs->onmessage(ws->channel, msg);
+        if (ws_service && ws_service->onmessage) {
+            ws_service->onmessage(ws->channel, msg);
         }
     }
 };

+ 5 - 2
http/server/HttpServer.cpp

@@ -221,7 +221,10 @@ static void on_recv(hio_t* io, void* _buf, int readbytes) {
         ws->parser->onMessage = std::bind(websocket_onmessage, std::placeholders::_1, std::placeholders::_2, io);
         // NOTE: cancel keepalive timer, judge alive by heartbeat.
         hio_set_keepalive_timeout(io, 0);
-        hio_set_heartbeat(io, HIO_DEFAULT_HEARTBEAT_INTERVAL, websocket_heartbeat);
+        if (handler->ws_service && handler->ws_service->ping_interval > 0) {
+            int ping_interval = MAX(handler->ws_service->ping_interval, 1000);
+            hio_set_heartbeat(io, ping_interval, websocket_heartbeat);
+        }
         // onopen
         handler->WebSocketOnOpen();
         return;
@@ -270,7 +273,7 @@ static void on_accept(hio_t* io) {
     http_server_t* server = (http_server_t*)hevent_userdata(io);
     handler->service = server->service;
     // ws
-    handler->ws_cbs = server->ws;
+    handler->ws_service = server->ws;
     // FileCache
     handler->files = default_filecache();
     hevent_set_userdata(io, handler);

+ 2 - 2
http/server/HttpServer.h

@@ -4,7 +4,7 @@
 #include "hexport.h"
 #include "HttpService.h"
 
-struct WebSocketServerCallbacks;
+struct WebSocketService;
 typedef struct http_server_s {
     char host[64];
     int port; // http_port
@@ -13,7 +13,7 @@ typedef struct http_server_s {
     int worker_processes;
     int worker_threads;
     HttpService* service;
-    WebSocketServerCallbacks* ws;
+    WebSocketService* ws;
     void* userdata;
 //private:
     int listenfd[2]; // 0: http, 1: https

+ 7 - 1
http/server/WebSocketServer.h

@@ -8,11 +8,17 @@
 #include "HttpServer.h"
 #include "WebSocketChannel.h"
 
-struct WebSocketServerCallbacks {
+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;
+    int ping_interval;
+
+    WebSocketService() {
+        ping_interval = 10000; // ms
+    }
 };
+#define WebSocketServerCallbacks WebSocketService // deprecated
 
 #define websocket_server_t      http_server_t
 #define websocket_server_run    http_server_run