Explorar o código

patch: websocket onmessage not given opcode

ithewei %!s(int64=3) %!d(string=hai) anos
pai
achega
5d6fd35c7f

+ 2 - 2
README.md

@@ -308,7 +308,7 @@ int main(int argc, char** argv) {
         printf("onopen: GET %s\n", url.c_str());
     };
     ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
-        printf("onmessage: %s\n", msg.c_str());
+        printf("onmessage: %.*s\n", (int)msg.size(), msg.data());
     };
     ws.onclose = [](const WebSocketChannelPtr& channel) {
         printf("onclose\n");
@@ -335,7 +335,7 @@ int main(int argc, char** argv) {
         printf("onopen\n");
     };
     ws.onmessage = [](const std::string& msg) {
-        printf("onmessage: %s\n", msg.c_str());
+        printf("onmessage: %.*s\n", (int)msg.size(), msg.data());
     };
     ws.onclose = []() {
         printf("onclose\n");

+ 4 - 3
examples/websocket_client_test.cpp

@@ -23,12 +23,13 @@ int main(int argc, char** argv) {
     ws.onopen = []() {
         printf("onopen\n");
     };
+    ws.onmessage = [&ws](const std::string& msg) {
+        printf("onmessage(type=%s len=%d): %.*s\n", ws.opcode() == WS_OPCODE_TEXT ? "text" : "binary",
+            (int)msg.size(), (int)msg.size(), msg.data());
+    };
     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;

+ 4 - 3
examples/websocket_server_test.cpp

@@ -36,8 +36,9 @@ public:
     ~MyContext() {
     }
 
-    int handleMessage(const std::string& msg) {
-        printf("onmessage: %s\n", msg.c_str());
+    int handleMessage(const std::string& msg, enum ws_opcode opcode) {
+        printf("onmessage(type=%s len=%d): %.*s\n", opcode == WS_OPCODE_TEXT ? "text" : "binary",
+            (int)msg.size(), (int)msg.size(), msg.data());
         return msg.size();
     }
 
@@ -72,7 +73,7 @@ int main(int argc, char** argv) {
     };
     ws.onmessage = [](const WebSocketChannelPtr& channel, const std::string& msg) {
         MyContext* ctx = channel->getContext<MyContext>();
-        ctx->handleMessage(msg);
+        ctx->handleMessage(msg, channel->opcode);
     };
     ws.onclose = [](const WebSocketChannelPtr& channel) {
         printf("onclose\n");

+ 2 - 0
http/WebSocketChannel.h

@@ -99,6 +99,8 @@ protected:
         return write(sendbuf_.base, frame_size);
     }
 
+public:
+    enum ws_opcode  opcode;
 private:
     Buffer      sendbuf_;
     std::mutex  mutex_;

+ 1 - 0
http/client/WebSocketClient.cpp

@@ -122,6 +122,7 @@ int WebSocketClient::open(const char* _url, const http_headers& headers) {
                 ws_parser_.reset(new WebSocketParser);
                 // websocket_onmessage
                 ws_parser_->onMessage = [this, &channel](int opcode, const std::string& msg) {
+                    channel->opcode = (enum ws_opcode)opcode;
                     switch (opcode) {
                     case WS_OPCODE_CLOSE:
                         channel->close();

+ 2 - 0
http/client/WebSocketClient.h

@@ -21,6 +21,8 @@ public:
     std::function<void()> onopen;
     std::function<void()> onclose;
     std::function<void(const std::string& msg)> onmessage;
+    // PATCH: onmessage not given opcode
+    enum ws_opcode opcode() { return channel ? channel->opcode : WS_OPCODE_CLOSE; }
 
     WebSocketClient();
     ~WebSocketClient();

+ 1 - 0
http/server/HttpHandler.cpp

@@ -84,6 +84,7 @@ bool HttpHandler::SwitchWebSocket(hio_t* io) {
     ws_parser.reset(new WebSocketParser);
     ws_channel.reset(new hv::WebSocketChannel(io, WS_SERVER));
     ws_parser->onMessage = [this](int opcode, const std::string& msg){
+        ws_channel->opcode = (enum ws_opcode)opcode;
         switch(opcode) {
         case WS_OPCODE_CLOSE:
             ws_channel->close(true);