gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:27:17 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

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

Long polling is a variation of the traditional polling technique andallows emulation of an information push from a server to a client.With long polling, the client requests information from the server ina similar way to a normal poll. However, if the server does not haveany information available for the client, instead of sending an emptyresponse, the server holds the request and waits for some informationto be available. Once the information becomes available (or after asuitable timeout), a complete response is sent to the client. Theclient will normally then immediately re-request information from theserver, so that the server will almost always have an availablewaiting request that it can use to deliver data in response to anevent. In a web/AJAX context, long polling is also known as Cometprogramming.

澄清

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

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

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com