ASP.NET Core 簡介 SignalRIntroduction 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.

以下是 ASP.NET Core 的一些功能 SignalR :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