ASP.NET Core SignalR 简介Introduction to ASP.NET Core SignalR

什么是 SignalR?What is SignalR?

ASP.NET Core SignalR 是一种开放源代码库,可简化将实时 web 功能添加到应用程序的功能。ASP.NET Core SignalR is an open-source library that simplifies adding real-time web functionality to apps. 实时 web 功能使服务器端代码可以立即将内容推送到客户端。Real-time web functionality enables server-side code to push content to clients instantly.

适用于 SignalR :Good candidates for SignalR:

  • 需要从服务器进行高频率更新的应用。Apps that require high frequency updates from the server. 示例包括游戏、社交网络、投票、拍卖、地图和 GPS 应用。Examples are gaming, social networks, voting, auction, maps, and GPS apps.
  • 仪表板和监视应用。Dashboards and monitoring apps. 示例包括公司仪表板、即时销售更新或旅行警报。Examples include company dashboards, instant sales updates, or travel alerts.
  • 协作应用。Collaborative apps. 协作应用的示例包括白板应用和团队会议软件。Whiteboard apps and team meeting software are examples of collaborative apps.
  • 需要通知的应用。Apps that require notifications. 社交网络、电子邮件、聊天、游戏、旅行警报和很多其他应用都需使用通知。Social networks, email, chat, games, travel alerts, and many other apps use notifications.

SignalR提供一个 API,用于创建 (RPC) 的服务器到客户端远程过程调用SignalR provides an API for creating server-to-client remote procedure calls (RPC). Rpc 通过服务器端 .NET Core 代码从客户端调用 JavaScript 函数。The RPCs call JavaScript functions on clients from server-side .NET Core code.

下面是的某些功能 SignalR ASP.NET Core:Here are some features of SignalR for ASP.NET Core:

  • 自动处理连接管理。Handles connection management automatically.
  • 将消息同时发送到所有连接的客户端。Sends messages to all connected clients simultaneously. 例如,聊天室。For example, a chat room.
  • 向特定客户端或客户端组发送消息。Sends messages to specific clients or groups of clients.
  • 可缩放以处理不断增加的流量。Scales to handle increasing traffic.

源托管在 SignalR GitHub 上的存储库中。The source is hosted in a SignalR repository on GitHub.

传输Transports

SignalR支持以下用于按正常回退) (处理实时通信的技术:SignalR supports the following techniques for handling real-time communication (in order of graceful fallback):

SignalR自动选择服务器和客户端功能内的最佳传输方法。SignalR automatically chooses the best transport method that is within the capabilities of the server and client.

集线器Hubs

SignalR使用集线器在客户端和服务器之间进行通信。SignalR uses hubs to communicate between clients and servers.

中心是一种高级管道,它允许客户端和服务器分别调用方法。A hub is a high-level pipeline that allows a client and server to call methods on each other. SignalR自动处理跨计算机边界的调度,使客户端能够在服务器上调用方法,反之亦然。SignalR handles the dispatching across machine boundaries automatically, allowing clients to call methods on the server and vice versa. 可以将强类型参数传递给方法,从而启用模型绑定。You can pass strongly-typed parameters to methods, which enables model binding. SignalR提供了两个内置的集线器协议:基于 JSON 的文本协议和基于MessagePack的二进制协议。SignalR provides two built-in hub protocols: a text protocol based on JSON and a binary protocol based on MessagePack. 与 JSON 相比,MessagePack 通常会创建较小的消息。MessagePack generally creates smaller messages compared to JSON. 较早的浏览器必须支持XHR 级别 2 ,才能提供 MessagePack 协议支持。Older browsers must support XHR level 2 to provide MessagePack protocol support.

中心通过发送包含客户端方法的名称和参数的消息来调用客户端代码。Hubs call client-side code by sending messages that contain the name and parameters of the client-side method. 作为方法参数发送的对象将使用配置的协议进行反序列化。Objects sent as method parameters are deserialized using the configured protocol. 客户端尝试将名称与客户端代码中的方法匹配。The client tries to match the name to a method in the client-side code. 当客户端找到匹配项时,它将调用方法并向其传递反序列化的参数数据。When the client finds a match, it calls the method and passes to it the deserialized parameter data.

其他资源Additional resources