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). 從伺服器端.NET Core 程式碼,Rpc 會呼叫 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.


SignalR 支援數種技術來處理即時通訊:SignalR supports several techniques for handling real-time communications:

SignalR 會自動選擇最佳的傳輸方法內的伺服器和用戶端功能。SignalR automatically chooses the best transport method that is within the capabilities of the server and client.


使用 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 和二進位通訊協定為基礎MessagePackSignalR provides two built-in hub protocols: a text protocol based on JSON and a binary protocol based on MessagePack. MessagePack 通常會建立較小的訊息,相較於 JSON。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