我的websocket上可以进行流量控制吗? [英] Can I have flow control on my websockets?

查看:408
本文介绍了我的websocket上可以进行流量控制吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用websocket将以Go语言编写的服务器中的视频y图像传输到HTML页面的客户端。我在下面分享的经验是使用Chrome。

I am using websockets to transfer video-y images from a server, written in Go, to a client which is an HTML page. My experience shared below is with Chrome.

我通过网络套接字的onmessage处理程序接收图像。接收图像时,我可能需要异步完成许多任务才能显示图像。即使这些任务未完成,也可能触发另一个onmessage()。我不想将图像排入队列,因为这时我无法像服务器一样快地进行处理,并且因为显示旧图像没有意义。我也不想删除这些图像,我根本不想接收它们。

I receive images via the onmessage handler of the websocket. On reception of an image, I might need to complete a number of tasks asynchronously before I can display the image. Even if these tasks are not finished, yet another onmessage() may fire. I do not want to queue images since at this point I'm not able to proceed as rapidly as the server is proceeding and because there is no point in displaying old images. I also do not want to drop these images, I don't wan't to receive them at all.

客户端将使用传统的TCP连接,它只是停止从连接中读取。这将导致接收缓冲区被填满,接收窗口被关闭,并最终在服务器上暂停图像发送。客户端开始读取后,接收缓冲区将为空,接收窗口将打开,服务器将恢复传输。每当我的服务器开始发送图像时,它都会选择最新鲜的图像。这种最新的行为以及TCP的流控制在许多情况下可确保合理的行为。

Would the client use a traditional TCP connection, it would just stop reading from the connection. This would cause the receive buffer to be filled, the receive window to be closed and, eventually, pause sending of images at the server. As soon the client starts reading, the receive buffers would empty, the receive window would open and the server resumes transmitting. Every time my server starts to send an image, it picks the freshest one. This pick-the-freshest behaviour, together with the flow control of TCP assures reasonable behaviour in many cases.

是否有可能具有TCP的流控制功能?哪个websockets基于websockets?我对基于TCP的流控制且没有应用程序级流控制的解决方案特别感兴趣,因为这往往会导致不必要的额外延迟。

Is it possible to have the flow control features of TCP, on which websockets is based, with websockets? I'm especially interested in a solution that relies on TCP's flow control and no application level flow control since this tends to incur unwanted, additional latency.

推荐答案

我怀疑您要的是可能的。 WebSocket API规范中没有该功能的接口。但是,规范所做的概述是要求在使用WebSocket的脚本之外的后台管理基础套接字连接,以便该脚本不会被WebSocket操作阻止。当套接字接收入站数据时,它将数据包装在消息中,并将其排队以供WebSocket脚本处理。当消息保留在队列中等待脚本处理它们时,没有什么可以阻止套接字读取更多数据。

I doubt what you are asking for is possible. There is no interface for that functionality in the WebSocket API spec. What the spec does outline, however, is a requirement that the underlying socket connection be managed in the background outside of the script that is using the WebSocket, so that the script is not blocked by WebSocket actions. When the socket receives inbound data, it wraps the data inside of a message and queues it for the WebSocket script to process. There is nothing to block the socket from reading more data while messages remain in the queue waiting for the script to process them.

WebSocket是显式的。当消息到达时,发回一条消息以确认它。使服务器在发送下一条消息之前等待接收到该确认。

The only real flow control you can implement in a WebSocket is an explicit one. When a message arrives, send back a message to acknowledge it. Make the server wait to receive that ack before sending its next message.

这篇关于我的websocket上可以进行流量控制吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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