Skip to content

write_websocket 使用 gzip 时协议错误 #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
osabc opened this issue Dec 5, 2024 · 3 comments · Fixed by #661 or alibaba/yalantinglibs#846
Closed

write_websocket 使用 gzip 时协议错误 #660

osabc opened this issue Dec 5, 2024 · 3 comments · Fixed by #661 or alibaba/yalantinglibs#846

Comments

@osabc
Copy link
Contributor

osabc commented Dec 5, 2024

Error: Invalid WebSocket frame: RSV2 and RSV3 must be clear

co_await req.get_conn()->write_websocket("{\"code\":1,\"message\":\"hi!\"}");

c3c650180913246ce23b26abdd0e988f

@qicosmos
Copy link
Owner

qicosmos commented Dec 6, 2024

我稍微改造了一下测试代码:

  • 启用ssl
    image
  • 把server.async_start() 改成server.sync_start() 作为测试服务器。

启动postman 测试ws
image

测试结果正常。
你能不能重复我的测试看一下?

@osabc
Copy link
Contributor Author

osabc commented Dec 6, 2024

我用的就是 server.sync_start() 测试的

@osabc
Copy link
Contributor Author

osabc commented Dec 6, 2024

修复方法应该是 rsv1 而不是 rsv2

diff --git a/include/cinatra/websocket.hpp b/include/cinatra/websocket.hpp
index 50a6d79..b18b6f9 100644
--- a/include/cinatra/websocket.hpp
+++ b/include/cinatra/websocket.hpp
@@ -126,11 +126,11 @@ class websocket {
                                     bool is_client = true) {
     frame_header hdr{};
     hdr.fin = eof;
-    hdr.rsv1 = 0;
     if (need_compression)
-      hdr.rsv2 = 1;
+      hdr.rsv1 = 1;
     else
-      hdr.rsv2 = 0;
+      hdr.rsv1 = 0;
+    hdr.rsv2 = 0;
     hdr.rsv3 = 0;
     hdr.opcode = static_cast<uint8_t>(op);
     hdr.mask = is_client;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants