SignalR 内部如何工作?
谁能告诉我 SignalR 内部是如何以高级方式工作的?
我猜测它正在使用 Response.Flush 刷新数据,并且在客户端以一定的时间间隔发送 Ajax 请求。正确吗?
Can anyone let me know how SignalR works internally in a high level way?
I am guessing it is flushing the data using Response.Flush
and at client side it is sending Ajax requests at certain intervals. Is it correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,SignalR 是连接的抽象。它为您提供了通过该连接的两种编程模型(集线器和持久连接)。 SignalR 有一个传输的概念,每个传输决定数据如何发送/接收以及如何连接和断开连接。
SignalR 有一些内置传输:
SignalR 尝试选择服务器和客户端支持的“最佳”连接(您也可以强制它使用特定传输)。
这就是高水平。如果你想了解每种传输方式是如何实现的,可以查看 源代码。
每种传输方式还有客户端代码:
https://github.com/SignalR/SignalR/tree/master/ src/Microsoft.AspNet.SignalR.Client.JS
如果您特别询问长轮询传输的工作原理:
它会向异步等待信号响应的服务器发送 ajax 请求。当有信号或请求超时时,它从服务器返回并发送另一个请求,该过程继续。 (我留下了一些关于客户端如何跟踪它所看到的内容的详细信息,这样它就不会错过消息)
希望这能回答您的大部分问题。
No, SignalR is an abstraction over a connection. It gives you two programming models over that connection (hubs and persistent connections). SignalR has a concept of transports, each transport decides how data is sent/received and how it connects and disconnects.
SignalR has a few built in transports:
SignalR tries to choose the "best" connection supported by server and client (you can also force it to use a specific transport).
That's the high level. If you want to see how each transport is implemented, you can look at the source code.
There's also client code for each transport:
https://github.com/SignalR/SignalR/tree/master/src/Microsoft.AspNet.SignalR.Client.JS
If you're asking about how the long polling transport works in particular:
It sends an ajax request to the server that's waiting asynchronously for a signal to respond. When there is a signal or the request times out, it returns from the server and sends another request and the process continues. (I left some details out about how the client it keeps track of what it saw so it doesn't miss messages)
Hopefully that answers most of your question.
@davidfowl 已经回答了大部分。但是,提供有关传输行为差异的更多详细信息,特别是 WebSocket 和其他传输之间的差异;以下是一些要点。
@davidfowl has already answered the major portion. However, to provide some more details regarding the difference in behavior of transports, specifically between WebSocket and other transports; below are some points.