Explorar o código

fix #264: websocket server use channel->close(asyn=true)

ithewei %!s(int64=3) %!d(string=hai) anos
pai
achega
48d008b7a8
Modificáronse 3 ficheiros con 7 adicións e 3 borrados
  1. 4 0
      http/WebSocketChannel.h
  2. 1 1
      http/client/WebSocketClient.h
  3. 2 2
      http/server/HttpHandler.cpp

+ 4 - 0
http/WebSocketChannel.h

@@ -83,6 +83,10 @@ public:
         return write(WS_SERVER_PONG_FRAME, WS_SERVER_MIN_FRAME_SIZE);
     }
 
+    int close() {
+        return SocketChannel::close(type == WS_SERVER);
+    }
+
 protected:
     int sendFrame(const char* buf, int len, enum ws_opcode opcode = WS_OPCODE_BINARY, bool fin = true) {
         bool has_mask = false;

+ 1 - 1
http/client/WebSocketClient.h

@@ -25,7 +25,7 @@ public:
     enum ws_opcode opcode() { return channel ? channel->opcode : WS_OPCODE_CLOSE; }
 
     WebSocketClient(EventLoopPtr loop = NULL);
-    ~WebSocketClient();
+    virtual ~WebSocketClient();
 
     // url = ws://ip:port/path
     // url = wss://ip:port/path

+ 2 - 2
http/server/HttpHandler.cpp

@@ -112,7 +112,7 @@ bool HttpHandler::SwitchWebSocket(hio_t* io) {
         ws_channel->opcode = (enum ws_opcode)opcode;
         switch(opcode) {
         case WS_OPCODE_CLOSE:
-            ws_channel->close(true);
+            ws_channel->close();
             break;
         case WS_OPCODE_PING:
             // printf("recv ping\n");
@@ -141,7 +141,7 @@ bool HttpHandler::SwitchWebSocket(hio_t* io) {
         ws_channel->setHeartbeat(ping_interval, [this](){
             if (last_recv_pong_time < last_send_ping_time) {
                 hlogw("[%s:%d] websocket no pong!", ip, port);
-                ws_channel->close(true);
+                ws_channel->close();
             } else {
                 // printf("send ping\n");
                 ws_channel->sendPing();