WebSocket中的数据块(例如lastEventId)是否有任何ID? [英] Is there any ID for data chunks in WebSocket like lastEventId?

查看:240
本文介绍了WebSocket中的数据块(例如lastEventId)是否有任何ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WebSocket在浏览器(客户端)中的按钮按下事件中从服务器发送图像.WebSocket API的onmessage()方法中接收的图像数据具有Blob结构和几个块.问题在于短时间内发生了多个按钮按下事件,接收到的数据块乱序.

I am sending an image from server on a button press event in browser (client) using WebSockets. The image data received in onmessage() method of WebSocket API is in a Blob structure and in several chunks. The issue is on multiple button press events in a short period, the chunks being received are out of order.

在浏览器端是否有任何标识符可用于区分块?WebSocket事件"lastEventId"中有一个字段,但始终为空.

Is there any identifier that I can use to differentiate the chunks on browser side? There is a field in WebSocket event "lastEventId", but it is always empty.

socket.onmessage = (event) =>
{
    console.log(event.data); // Chunked Blob data
    console.log(event.lastEventId); // Always empty
    // Process data
}

OR

该服务器是 lighttpd服务器,那么是否需要对lighttpd进行任何配置才能为数据块启用某种ID?

The server is a lighttpd server so is there any configuration of lighttpd required to enable some kind of ID for the data chunks?

推荐答案

我认为您可能会误以为WebSocket与服务器发送事件

I think you may be mistaking WebSockets with Server-Sent-Events

服务器发送的事件与 WebSockets https://www.html5rocks.com/en/tutorials/eventsource/basics/#toc-introduction-differences

Server-Sent Events vs. WebSockets https://www.html5rocks.com/en/tutorials/eventsource/basics/#toc-introduction-differences

服务器发送的事件具有WebSocket设计上缺少的各种功能,例如自动重新连接,事件ID和发送任意事件的功能.

Server-Sent Events have a variety of features that WebSockets lack by design such as automatic reconnection, event IDs, and the ability to send arbitrary events.

如果服务器端脚本是websocket终结点,则可以在服务器发送的事件响应中包含一个id,并以websocket框架的形式正确发送.

If your server-side script is the websocket endpoint, then you can include an id with your Server-Sent Events response, and send it properly framed in websocket frames.

如果您正在使用lighttpd mod_wstunnel 执行通用的WebSocket框架,则您将必须在脚本中适当地构造Blob数据,然后客户端将可能必须从多个WebSocket框架中重建Blob.lighttpd mod_wstunnel与JSON有效负载配合得很好,因此您可以考虑发送(小的!)图像作为base64编码JSON的一部分.这绝不是唯一的方法.

If you are using lighttpd mod_wstunnel to perform generic websocket framing, then you will have to appropriately structure the blob data in your script, and then the client will have to reconstruct the blob, potentially from multiple websocket frames. lighttpd mod_wstunnel works quite well with a JSON payload, so you might consider sending (small!) images as part of base64-encoded JSON. This is by no means the only way to do this.

有关服务器发送的事件

这篇关于WebSocket中的数据块(例如lastEventId)是否有任何ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆