简体   繁体   English

在多个框架中拆分 websocket 消息

[英]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.

相关问题 警告:无法发送消息。 websocket 不可用 - warning: Cannot send message. websocket is not available 如何在 helm 图表中将 if 语句拆分为多行 - how split if statement over multiple lines in helm chart 是否可以在带有可扩展选项的推送通知中发送 Firebase(FCM) 通知消息(多行消息); 使用 Python? - Is it possible to send Firebase(FCM) notification message(multiple lined message) in the push notification with expandable option on it; using Python? 使用谷歌大查询 sql 将一列中的字符串拆分为多列而不断词 - Using Google big query sql split the string in a column to multiple columns without breaking words 如何使用 Package whatsapp_unilink 在单个文本消息中传递多个数据 - How do I pass Multiple Data in a single text message using Package whatsapp_unilink AWS websocket 容器 cdk ecs - AWS websocket container cdk ecs AWS API 网关 Websocket 未知错误 - AWS API Gateway Websocket UnknownError 无法通过 amazon connect 的 websocket 进行通信 - Not able to communicate through websocket from amazon connect 字符串拆分 Golang - String Split Golang 从 gorrila websocket 读写时是否创建了新线程? - Is a new thread created while reading and writing from gorrila websocket?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM