[英]Split websocket message in multiple frames
I am using native javascript websocket in browser and we have an application hosted on AWS where every request goes through API gateway.我在浏览器中使用本机 javascript websocket,我们在 AWS 上托管了一个应用程序,每个请求都通过 API 网关。 In some cases, request data is going upto 60kb, and then my websocket connection is closing automatically.
在某些情况下,请求数据高达 60kb,然后我的 websocket 连接自动关闭。 In AWS documentation, I found out below explanation of this issue
在 AWS 文档中,我发现了以下对此问题的解释
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html
API Gateway supports message payloads up to 128 KB with a maximum frame size of 32 KB. API Gateway 支持高达 128 KB 的消息负载,最大帧大小为 32 KB。 If a message exceeds 32 KB, you must split it into multiple frames, each 32 KB or smaller.
如果消息超过 32 KB,则必须将其拆分为多个帧,每个帧不超过 32 KB。 If a larger message is received, the connection is closed with code 1009.
如果收到更大的消息,连接将关闭,代码为 1009。
I tried to find how I can split a message in multiple frames using native javascript websocket but could not find any config related to frames in documentation or anywhere else我试图找到如何使用本机 javascript websocket 将消息拆分为多个帧,但在文档或其他任何地方都找不到与帧相关的任何配置
Although I find something related to message fragmentation but it seems like a custom solution that I need to implement at both frontend and backend https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#message_fragmentation虽然我发现了一些与消息碎片相关的东西,但它似乎是我需要在前端和后端实现的自定义解决方案https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#message_fragmentation
As far as I know, you cannot do this using the JS AWS SDK "postToConnection" API.据我所知,您不能使用 JS AWS SDK“postToConnection”API 执行此操作。 Best you can do is write your own poor's man fragmentation and send the chunks as independent messages.
您能做的最好的事情就是编写自己的 poor's man 碎片并将这些块作为独立消息发送。
const splitInChunks =
(sizeInBytes: number) =>
(buffer: Buffer): Buffer[] => {
const size = Buffer.byteLength(buffer);
let start = 0;
let end = sizeInBytes;
const chunks: Buffer[] = [];
do {
chunks.push(buffer.subarray(start, end));
start += sizeInBytes;
end += sizeInBytes;
} while (start < size);
return chunks;
};
Where sizeInBytes
must be smaller than 32KB.其中
sizeInBytes
必须小于 32KB。 Then you iterate over the chunks:然后你遍历这些块:
await Promise.all(chunks.map(c => apiGatewayClient.postToConnection({ data: JSON.stringify(c), connectionId: myConnectionId })
Which may run into rate limits depending on the number of chunks, so consider sending the requests serially and not in parallel根据块的数量,这可能会遇到速率限制,因此请考虑串行而不是并行发送请求
Final remark: Buffer.prototype.subarray
is very efficient because it does not reallocate memory: the new chunks point at the same memory space of the original buffer.最后说明:
Buffer.prototype.subarray
非常高效,因为它不会重新分配内存:新块指向与原始缓冲区相同的内存空间。 Think pointer arithmetic in C.想想 C 中的指针运算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.