Skip to content
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

websocket_server_write_frame_header函数建议返回头部长度 #22

Open
wangyongxiao opened this issue May 27, 2021 · 3 comments
Open

Comments

@wangyongxiao
Copy link

wangyongxiao commented May 27, 2021

websocket_server_write_frame_header函数建议返回头部长度,这样使用完这个函数可以得到dst应该偏移多少,比如如下

static ngx_inline int
websocket_server_write_frame_header(u_char *dst, u_char opcode,
size_t payload_length)
{
dst[0] = (u_char)((opcode & 0x0F) | 0x80);
if ( payload_length <= 125 ) {
dst[1] = (u_char)(payload_length);

return 2;

}

dst[1] = (u_char) 126;
*(u_short *)&(dst[2]) = htons((u_short)(payload_length));

return 4;
}

使用时:
...
int len = websocket_server_write_frame_header(dst, opcode, payload_length);
dst += len;
...

@tg123
Copy link
Owner

tg123 commented May 27, 2021

有个macro 用来计算 websocket_server_encoded_header_length 不过返回没有坏处

@wangyongxiao
Copy link
Author

问个问题,为什么不补充当数据长度大于65535的情况呢,我在基于nginx-http-flv-module模块扩展websocket-flv功能时借鉴了websockify-nginx-module模块,遇到了数据长度大于65535,把这个函数做了修改让他支持数据长度大于65535的情况

@tg123
Copy link
Owner

tg123 commented May 28, 2021

多于65535 会自动拆成 多个 websocket frame 自动
实践上 如果过大 浏览器 端 也不好处理 容易出现卡顿

这个值相当于MTU 个人觉得目前的大小足够了

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

No branches or pull requests

2 participants