c# - 长轮询实际上如何在客户端调用回调?

标签 c# wcf long-polling basichttpbinding

客户端启动长轮询,调用服务器上的方法并传入 AsyncCallback 实例,其中包含回调委托(delegate),当服务器异步返回客户端时将调用该委托(delegate)。

现在我对此的理解是有限的,但似乎在 BasicHttp WCF 中,AsyncCallback 参数被序列化并发送到服务器,然后反序列化它,缓存它并最终调用它以“返回”客户端。

首先,以上解释是否正确?其次,客户端的AsyncCallback是如何跨网络一路调用的?

最佳答案

连接保持打开状态,以便服务器通过现有连接进行响应,包括响应中的回调处理程序名称。

客户端了解消息的格式,然后可以使用来自服务器响应的数据调用适当的本地方法(基于回调处理程序)。

我通常不喜欢引用维基百科,但在这种情况下,它是 not a bad explanation长轮询...

Long polling is a variation of the traditional polling technique and allows emulation of an information push from a server to a client. With long polling, the client requests information from the server in a similar way to a normal poll. However, if the server does not have any information available for the client, instead of sending an empty response, the server holds the request and waits for some information to be available. Once the information becomes available (or after a suitable timeout), a complete response is sent to the client. The client will normally then immediately re-request information from the server, so that the server will almost always have an available waiting request that it can use to deliver data in response to an event. In a web/AJAX context, long polling is also known as Comet programming.

澄清

  • 客户端向服务器发送 POST,包括回调句柄并保持连接打开
  • 一段时间后,服务器使用来自 POST 的回调句柄和响应数据进行响应(这发生在您在服务器上调用 AsyncCallback 的方法时)
  • 客户端从服务器读取响应,识别已返回的回调句柄并使用它来识别要执行的方法,
  • 客户端执行回调句柄指定的方法并传入服务器响应的其余部分。

这类似于 JSONP 的工作方式(回调部分,不是长轮询),如果您熟悉的话?本质上,回调句柄仅传递给服务器,以便它可以与响应一起发回并允许客户端调用正确的方法。

还有一些额外的检查在幕后进行,以确保只调用预期的方法,并且恶意服务器不能只执行它在客户端代码中选择的任何方法。

关于c# - 长轮询实际上如何在客户端调用回调?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13629430/

相关文章:

php - Comet 软件相对于标准长轮询的优势

c# - ASP.NET - 模型不存在

c# - 用虚拟方法覆盖抽象方法

c# - 序列不包含任何元素?

.net - 在运行时指定 WCF 终结点的 IP 地址

c# - 自定义终结点行为未在具有服务引用的 WCF 客户端中使用

wcf - restart/iisreset 禁用 NET.TCP 协议(protocol)?

c# - C# FFMPEG 我得到 MPEG2 和 MPEG4 格式的错误?

c# - 使用 iOS 客户端应用程序和 C# 服务器进行长轮询

node.js - 如何处理来自SQS的多条消息?