SignalR 内部如何工作?

发布于 2024-12-10 16:28:28 字数 102 浏览 0 评论 0原文

谁能告诉我 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏夜暖风 2024-12-17 16:28:28

不,SignalR 是连接的抽象。它为您提供了通过该连接的两种编程模型(集线器和持久连接)。 SignalR 有一个传输的概念,每个传输决定数据如何发送/接收以及如何连接和断开连接。

SignalR 有一些内置传输:

  1. WebSockets
  2. 服务器发送事件
  3. 永久帧
  4. 长轮询

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:

  1. WebSockets
  2. Server Sent Events
  3. Forever Frame
  4. Long polling

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.

女皇必胜 2024-12-17 16:28:28

@davidfowl 已经回答了大部分。但是,提供有关传输行为差异的更多详细信息,特别是 WebSocket 和其他传输之间的差异;以下是一些要点。

  • WebSocket 是唯一在客户端和服务器之间建立真正持久的双向连接的传输。不过,WebSocket 仅受 IIS 8 或更高版本以及最新版本的 Internet Explorer、Google Chrome 和 Mozilla Firefox 支持。
  • 虽然服务器发送事件、永远帧和长轮询,这三者都遵循单向通信,并且受到大多数浏览器的支持。

@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.

  • WebSocket is the only transport that establishes a true persistent, two-way connection between client and server. However, WebSocket is supported only by IIS 8 or above, and the latest versions of Internet Explorer, Google Chrome and Mozilla Firefox.
  • While Server Sent Events, Forever Frame and Long polling, all three follow a one-way communication, and are supported by most of the browsers.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文