SlideShare uma empresa Scribd logo
1 de 35
利用Signalr打造即時通訊 
高榮章2014-09-10 
1 
BLOG: 
http://trufflepenne.blogspot.tw/
在這之前 
談談SignalR 
Live demo 
升級到SignalR 2 
Performance Tuning 
擴充(Scaling out) 
Agenda 
2
[在這之前] 
即時通訊的寫法: 
Socket WCF 雙向(Duplex) 
3
Web的運作 
網頁基於Http的運作,發起者都是Client ,差別在於需不需要Postback 
一般Ajax 
Client Server 
要求 
回覆網頁 
處理 
要求 
回覆網頁 
要求 
回覆網頁 
處理 
處理 
Client Server 
要求 
回覆網頁 
處理 
要求 
回覆網頁 
要求 
回覆網頁 
處理 
處理 
4
如果要在Web上實作即時通訊:聊天室? 
Pulling是好方法? 
是很簡單 
但代價太高 
大部分時間是虛工 
5
Html 5的Web socket帶來一道曙光 
persistent connection 
two-way channel 
6 
小明Server 
Connect 
明天Tech day哪等? 
市政府捷運站 
好 
大雄 
Connect 
明天Tech day哪等? 
市政府捷運站 
好 
Process 
Process 
Process 
Process 
Connection
即時通訊仍有一些麻煩事 
持續連線要自行處理 
處理訂閱 
斷線偵測 
偵測狀態改變 
訊息要自行解析 
如果要進行網頁和Win form 互傳? 
不支援WebSocket怎麼辦 
7
[談談SignalR] 
用戶伺服器 
Client Hub 
Client 
Connection 
Message Group 
8
SignalR架構 
Hubs 
Persistent connections 
Transports 
Forever frame Long pooling Server-Event Websoket 
Internet Protocols 
虛擬層級 
9
程式概念 
10
SignalR Push通訊類型 
• Html 5 
Websoket 
Server-Send Event 
• Comet 
Long pooling 
Forever frame 
11
Server-send Event 
12 
小明Server 
Connect 
Event 
Event 
Process 
Process 
Process 
Process 
Connection 
Event
Long Pulling 
13 
小明Server 
有新訊息? 
明天Tech day坐捷運去 
0900到 
大雄 
明天Tech day坐捷運去? 
0900到 
H 
T 
T 
P 
有新訊息? 
H 
T 
T 
P
Forever frame 
14 
小明Server 
GET/Forever 
<script> 
addMsg(1," 
明天Tech day坐捷運去 
:); 
</script> 
<script> 
addMsg(1," 
0900到:); 
</script> 
大雄 
明天Tech day坐捷運去? 
0900到 
H 
T 
T 
P 
HTTP 
HTTP
適用平台(1) 
Browser 
Windows Desktop 
15
適用平台(2) 
Windows Store App/Phone app 
16
SignalR 2 快速上手 
• 安裝.net 4.5 
• 新增一個空白asp.net專案 
• 套件管理器主控台 
install-package Microsoft.AspNet.SignalR 
Install-Package json2 17
Hub(1) 
• Host 
• IIS:ASP.NET 
• Self: Windows Service with Owin 
• 新增項目, 選SignalR Hub類別(v2) 
using System; 
using System.Web; 
using Microsoft.AspNet.SignalR; 
namespace SignalRChat 
{ 
public class ChatHub : Hub 
{ 
public void Send(string name, string message) 
{ 
// Call the broadcastMessage method to update clients. 
Clients.All.broadcastMessage(name, message); 
} 
} 
} 
18
Hub(2) 
• 新增startup項目, 選OWIN 啟動類別 
using Microsoft.Owin; 
using Owin; 
[assembly: OwinStartup(typeof(SignalRChat.Startup))] 
namespace SignalRChat 
{ 
public class Startup 
{ 
public void Configuration(IAppBuilder app) 
{ 
app.MapSignalR(); 
} 
} 
} 
19
Client(1) 
• 新增一個Html 
20 
<!DOCTYPE html> 
<html> 
<head> 
<title>SignalR Simple Chat</title> 
<style type="text/css"> 
.container { 
background-color: #99CCFF; 
border: thick solid #808080; 
padding: 20px; 
margin: 20px; 
} 
</style> 
</head> 
<body> 
<div class="container"> 
<input type="text" id="message" /> 
<input type="button" id="sendmessage" value="Send" /> 
<input type="hidden" id="displayname" /> 
<ul id="discussion"> 
</ul> 
</div> 
<!--Script references. --> 
<!--Reference the jQuery library. --> 
<script src="Scripts/jquery-1.6.4.min.js" ></script> 
<!--Reference the SignalR library. --> 
<script src="Scripts/jquery.signalR-2.1.0.min.js"></script> 
<!--Reference the autogenerated SignalR hub script. --> 
<script src="signalr/hubs"></script>
Client(2) 
21 
<!--Add script to update the page and send messages.--> 
<script type="text/javascript"> 
$(function () { 
// Declare a proxy to reference the hub. 
var chat = $.connection.chatHub; 
// Create a function that the hub can call to broadcast messages. 
chat.client.broadcastMessage = function (name, message) { 
// Html encode display name and message. 
var encodedName = $('<div />').text(name).html(); 
var encodedMsg = $('<div />').text(message).html(); 
// Add the message to the page. 
$('#discussion').append('<li><strong>' + encodedName 
+ '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); 
}; 
// Get the user name and store it to prepend to messages. 
$('#displayname').val(prompt('Enter your name:', '')); 
// Set initial focus to message input box. 
$('#message').focus(); 
// Start the connection. 
$.connection.hub.start().done(function () { 
$('#sendmessage').click(function () { 
// Call the Send method on the hub. 
chat.server.send($('#displayname').val(), $('#message').val()); 
// Clear text box and reset focus for next comment. 
$('#message').val('').focus(); 
}); 
}); 
}); 
</script> 
</body> 
</html>
[Live demo] 
22
[升級到SignalR 2](1) 
• 2.0持續使用OWIN 
• Target設為4.5 
• 套件管理器主控台 
移除1.1 
>Uninstall-Package Microsoft.AspNet.SignalR –RemoveDependencies 
安裝2.0 
>Install-Package Microsoft.AspNet.SignalR 
• Client js lib改為2.1 
<!--Reference the SignalR library. --> <script 
src="Scripts/jquery.signalR-2.1.0.min.js"></script> 
23
升級到SignalR 2(2) 
• Global.ascx.cs移除RouteTable 
protected void Application_Start(object sender, EventArgs e) 
{ 
RouteTable.Routes.MapHubs(); 
} 
• 新增Owin startup 
24
升級到SignalR 2(3) 
• Startup.cs填入以下code 
using Microsoft.Owin; 
using Owin; 
[assembly: OwinStartup(typeof(SignalRChat.Startup))] 
namespace SignalRChat 
{ 
public class Startup 
{ 
public void Configuration(IAppBuilder app) 
{ 
app.MapSignalR(); 
} 
} 
} 
• Hub.cs 增加Using 
using Microsoft.AspNet.SignalR; 
25
[Performance Tuning](1) 
https://github.com/SignalR/SignalR/wiki/Performance 
• IIS Configuration 
Max concurrent requests per applicationm: 預設5000提升到100,000 
cd %windir%System32inetsrv 
appcmd.exe set config /section:system.webserver/serverRuntime 
/appConcurrentRequestLimit:100000 
26
[Performance Tuning](2) 
• ASP.NET Configuration 
XmaxConCurrentRequestPerCPU: default 5000, 可以提到20000 
打開%windir%Microsoft.NETFrameworkv4.0.30319aspnet.config 
(Framework64 for 64 bit processes) 
<?xml version="1.0" encoding="UTF-8" ?> 
<configuration> 
<runtime> 
<legacyUnhandledExceptionPolicy enabled="false" /> 
<legacyImpersonationPolicy enabled="true"/> 
<alwaysFlowImpersonationPolicy enabled="false"/> 
<SymbolReadingPolicy enabled="1" /> 
<shadowCopyVerifyByTimestamp enabled="true"/> 
</runtime> 
<startup useLegacyV2RuntimeActivationPolicy="true" /> 
<system.web> 
<applicationPool maxConcurrentRequestsPerCPU="20000" /> 
</system.web> 
</configuration> 
Request Qeue Limit: 可以提到250000 
打開%windir%Microsoft.NETFrameworkv4.0.30319Configmachine.config 
<processModel autoConfig="false" requestQueueLimit="250000" /> 
27
[Performance Tuning](3) 
• 減少訊息頻率 
• 減低訊息大小:使用JasonIgnore 
using Newtonsoft.Json; 
using System; 
public class ShapeModel 
{ 
[JsonProperty("l")] 
public double Left { get; set; } 
[JsonProperty("t")] 
public double Top { get; set; } 
// We don't want the client to get the "LastUpdatedBy" property 
[JsonIgnore] 
public string LastUpdatedBy { get; set; } 
} 
28
[Performance Tuning](4) 
• SignalR configuration setting 
DefaultMessageBufferSize :1000, 如果訊息過大, 可調降該值, ex:500 
public class Startup 
{ 
public void Configuration(IAppBuilder app) 
{ 
// Any connection or hub wire up and configuration should go here 
GlobalHost.Configuration.DefaultMessageBufferSize = 500; 
app.MapSignalR(); 
} 
} 
29
[Performance Tuning](5) 
• 測試SignalR負載工具-Cran 
https://github.com/SignalR/SignalR/tree/dev/src/Microsoft.AspNet.S 
ignalR.Crank 
• SignalR performance counter 
管理Nuget套件, 搜尋signalr.utils 
30
[Performance Tuning](6) 
路徑:專案路經/packages/Microsoft.AspNet.SignalR.Utils.<version>/tools 
安裝> SignalR.exe ipc 
反安裝> SignalR.exe upc 
Windows 計數器> perfmon 
31
Fiddler 
32
擴充(Scaling out) 
• Sqlserver 
• Redius 
• Azure survice bus 
http://www.asp.net/signalr/overview/signalr-20/performance-and-scaling/scaleout-in-signalr 
33
Signal R 2教學資源 
http://www.asp.net/signalr/overview/signalr-20 
34
35

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

大規模微服務導入 - #2 從零開始的微服務 .NET Core 框架設計
大規模微服務導入 - #2 從零開始的微服務 .NET Core 框架設計大規模微服務導入 - #2 從零開始的微服務 .NET Core 框架設計
大規模微服務導入 - #2 從零開始的微服務 .NET Core 框架設計
 
Angular js twmvc#17
Angular js twmvc#17Angular js twmvc#17
Angular js twmvc#17
 
twMVC#41 hololens2 MR
twMVC#41 hololens2 MRtwMVC#41 hololens2 MR
twMVC#41 hololens2 MR
 
動手打造 application framework-twMVC#15
動手打造 application framework-twMVC#15動手打造 application framework-twMVC#15
動手打造 application framework-twMVC#15
 
Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹Asp.net mvc 概觀介紹
Asp.net mvc 概觀介紹
 
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
使用 TypeScript 駕馭 Web 世界的脫韁野馬:以 Angular 2 開發框架為例
 
twMVC#19 | 微信公眾平台申請與wechat api 開發血淚史
twMVC#19 | 微信公眾平台申請與wechat api 開發血淚史twMVC#19 | 微信公眾平台申請與wechat api 開發血淚史
twMVC#19 | 微信公眾平台申請與wechat api 開發血淚史
 
Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸Angular 4 新手入門攻略完全制霸
Angular 4 新手入門攻略完全制霸
 
twMVC#23 | 快速上手 Azure Functions
twMVC#23 | 快速上手 Azure FunctionstwMVC#23 | 快速上手 Azure Functions
twMVC#23 | 快速上手 Azure Functions
 
容器驅動開發 - .NET Conf 2017 @ 台中
容器驅動開發 - .NET Conf 2017 @ 台中容器驅動開發 - .NET Conf 2017 @ 台中
容器驅動開發 - .NET Conf 2017 @ 台中
 
Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)Micro-frontends with Angular 10 (Modern Web 2020)
Micro-frontends with Angular 10 (Modern Web 2020)
 
twMVC#19 | opserver監控服務的解決
twMVC#19 | opserver監控服務的解決twMVC#19 | opserver監控服務的解決
twMVC#19 | opserver監控服務的解決
 
KSDG-ASP.NET MVC 5 Overview (偽三國誌)
KSDG-ASP.NET MVC 5 Overview (偽三國誌)KSDG-ASP.NET MVC 5 Overview (偽三國誌)
KSDG-ASP.NET MVC 5 Overview (偽三國誌)
 
twMVC#42 讓我們用一種方式來開發吧
twMVC#42 讓我們用一種方式來開發吧twMVC#42 讓我們用一種方式來開發吧
twMVC#42 讓我們用一種方式來開發吧
 
twMVC#31沒有 hdd 的網站重構 webform to mvc
twMVC#31沒有 hdd 的網站重構 webform to mvctwMVC#31沒有 hdd 的網站重構 webform to mvc
twMVC#31沒有 hdd 的網站重構 webform to mvc
 
專案分層架構 twMVC#18
專案分層架構 twMVC#18專案分層架構 twMVC#18
專案分層架構 twMVC#18
 
Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)Visual Studio 2017 新功能探索 (Study4.TW)
Visual Studio 2017 新功能探索 (Study4.TW)
 
ASP.NET Core 3.0 新功能
ASP.NET Core 3.0 新功能ASP.NET Core 3.0 新功能
ASP.NET Core 3.0 新功能
 
一小時可以打造什麼服務Plus twMVC#18
一小時可以打造什麼服務Plus twMVC#18一小時可以打造什麼服務Plus twMVC#18
一小時可以打造什麼服務Plus twMVC#18
 
ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索ASP.NET Core 6.0 全新功能探索
ASP.NET Core 6.0 全新功能探索
 

Semelhante a 利用Signalr打造即時通訊@Tech day geek

透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps
Eric ShangKuan
 
Node Web开发实战
Node Web开发实战Node Web开发实战
Node Web开发实战
fengmk2
 
Axis1.4 开发指南 V1.0
Axis1.4 开发指南 V1.0Axis1.4 开发指南 V1.0
Axis1.4 开发指南 V1.0
yiditushe
 
HTML5概览
HTML5概览HTML5概览
HTML5概览
Adam Lu
 
深入浅出Netty l.t
深入浅出Netty   l.t深入浅出Netty   l.t
深入浅出Netty l.t
oleone
 
使用NodeJS构建静态资源管理系统
使用NodeJS构建静态资源管理系统使用NodeJS构建静态资源管理系统
使用NodeJS构建静态资源管理系统
Frank Xu
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送
yongboy
 

Semelhante a 利用Signalr打造即時通訊@Tech day geek (20)

[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
 
透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps透過 Windows Azure Mobile Services 開發各平台 Apps
透過 Windows Azure Mobile Services 開發各平台 Apps
 
PHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB ServerPHP 應用之一 socket funion : 偽 WEB Server
PHP 應用之一 socket funion : 偽 WEB Server
 
Node Web开发实战
Node Web开发实战Node Web开发实战
Node Web开发实战
 
Axis1.4 开发指南 V1.0
Axis1.4 开发指南 V1.0Axis1.4 开发指南 V1.0
Axis1.4 开发指南 V1.0
 
构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接 构建ActionScript游戏服务器,支持超过15000并发连接
构建ActionScript游戏服务器,支持超过15000并发连接
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展
 
HTML5概览
HTML5概览HTML5概览
HTML5概览
 
Node Web开发实战
Node Web开发实战Node Web开发实战
Node Web开发实战
 
twMVC#44 讓我們用 k6 來進行壓測吧
twMVC#44 讓我們用 k6 來進行壓測吧twMVC#44 讓我們用 k6 來進行壓測吧
twMVC#44 讓我們用 k6 來進行壓測吧
 
张所勇:前端开发工具推荐
张所勇:前端开发工具推荐张所勇:前端开发工具推荐
张所勇:前端开发工具推荐
 
深入浅出Netty l.t
深入浅出Netty   l.t深入浅出Netty   l.t
深入浅出Netty l.t
 
Node.js长连接开发实践
Node.js长连接开发实践Node.js长连接开发实践
Node.js长连接开发实践
 
高性能远程调用解决方案
高性能远程调用解决方案高性能远程调用解决方案
高性能远程调用解决方案
 
使用NodeJS构建静态资源管理系统
使用NodeJS构建静态资源管理系统使用NodeJS构建静态资源管理系统
使用NodeJS构建静态资源管理系统
 
Asp.net 5 新功能與變革
Asp.net 5 新功能與變革Asp.net 5 新功能與變革
Asp.net 5 新功能與變革
 
Bluemix Node-Red Part I
Bluemix Node-Red Part IBluemix Node-Red Part I
Bluemix Node-Red Part I
 
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
從 Web Site 到 Web Application,從 Web Services 到 Mobile Services
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送
 

利用Signalr打造即時通訊@Tech day geek

  • 1. 利用Signalr打造即時通訊 高榮章2014-09-10 1 BLOG: http://trufflepenne.blogspot.tw/
  • 2. 在這之前 談談SignalR Live demo 升級到SignalR 2 Performance Tuning 擴充(Scaling out) Agenda 2
  • 4. Web的運作 網頁基於Http的運作,發起者都是Client ,差別在於需不需要Postback 一般Ajax Client Server 要求 回覆網頁 處理 要求 回覆網頁 要求 回覆網頁 處理 處理 Client Server 要求 回覆網頁 處理 要求 回覆網頁 要求 回覆網頁 處理 處理 4
  • 6. Html 5的Web socket帶來一道曙光 persistent connection two-way channel 6 小明Server Connect 明天Tech day哪等? 市政府捷運站 好 大雄 Connect 明天Tech day哪等? 市政府捷運站 好 Process Process Process Process Connection
  • 7. 即時通訊仍有一些麻煩事 持續連線要自行處理 處理訂閱 斷線偵測 偵測狀態改變 訊息要自行解析 如果要進行網頁和Win form 互傳? 不支援WebSocket怎麼辦 7
  • 8. [談談SignalR] 用戶伺服器 Client Hub Client Connection Message Group 8
  • 9. SignalR架構 Hubs Persistent connections Transports Forever frame Long pooling Server-Event Websoket Internet Protocols 虛擬層級 9
  • 11. SignalR Push通訊類型 • Html 5 Websoket Server-Send Event • Comet Long pooling Forever frame 11
  • 12. Server-send Event 12 小明Server Connect Event Event Process Process Process Process Connection Event
  • 13. Long Pulling 13 小明Server 有新訊息? 明天Tech day坐捷運去 0900到 大雄 明天Tech day坐捷運去? 0900到 H T T P 有新訊息? H T T P
  • 14. Forever frame 14 小明Server GET/Forever <script> addMsg(1," 明天Tech day坐捷運去 :); </script> <script> addMsg(1," 0900到:); </script> 大雄 明天Tech day坐捷運去? 0900到 H T T P HTTP HTTP
  • 16. 適用平台(2) Windows Store App/Phone app 16
  • 17. SignalR 2 快速上手 • 安裝.net 4.5 • 新增一個空白asp.net專案 • 套件管理器主控台 install-package Microsoft.AspNet.SignalR Install-Package json2 17
  • 18. Hub(1) • Host • IIS:ASP.NET • Self: Windows Service with Owin • 新增項目, 選SignalR Hub類別(v2) using System; using System.Web; using Microsoft.AspNet.SignalR; namespace SignalRChat { public class ChatHub : Hub { public void Send(string name, string message) { // Call the broadcastMessage method to update clients. Clients.All.broadcastMessage(name, message); } } } 18
  • 19. Hub(2) • 新增startup項目, 選OWIN 啟動類別 using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(SignalRChat.Startup))] namespace SignalRChat { public class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } } 19
  • 20. Client(1) • 新增一個Html 20 <!DOCTYPE html> <html> <head> <title>SignalR Simple Chat</title> <style type="text/css"> .container { background-color: #99CCFF; border: thick solid #808080; padding: 20px; margin: 20px; } </style> </head> <body> <div class="container"> <input type="text" id="message" /> <input type="button" id="sendmessage" value="Send" /> <input type="hidden" id="displayname" /> <ul id="discussion"> </ul> </div> <!--Script references. --> <!--Reference the jQuery library. --> <script src="Scripts/jquery-1.6.4.min.js" ></script> <!--Reference the SignalR library. --> <script src="Scripts/jquery.signalR-2.1.0.min.js"></script> <!--Reference the autogenerated SignalR hub script. --> <script src="signalr/hubs"></script>
  • 21. Client(2) 21 <!--Add script to update the page and send messages.--> <script type="text/javascript"> $(function () { // Declare a proxy to reference the hub. var chat = $.connection.chatHub; // Create a function that the hub can call to broadcast messages. chat.client.broadcastMessage = function (name, message) { // Html encode display name and message. var encodedName = $('<div />').text(name).html(); var encodedMsg = $('<div />').text(message).html(); // Add the message to the page. $('#discussion').append('<li><strong>' + encodedName + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>'); }; // Get the user name and store it to prepend to messages. $('#displayname').val(prompt('Enter your name:', '')); // Set initial focus to message input box. $('#message').focus(); // Start the connection. $.connection.hub.start().done(function () { $('#sendmessage').click(function () { // Call the Send method on the hub. chat.server.send($('#displayname').val(), $('#message').val()); // Clear text box and reset focus for next comment. $('#message').val('').focus(); }); }); }); </script> </body> </html>
  • 23. [升級到SignalR 2](1) • 2.0持續使用OWIN • Target設為4.5 • 套件管理器主控台 移除1.1 >Uninstall-Package Microsoft.AspNet.SignalR –RemoveDependencies 安裝2.0 >Install-Package Microsoft.AspNet.SignalR • Client js lib改為2.1 <!--Reference the SignalR library. --> <script src="Scripts/jquery.signalR-2.1.0.min.js"></script> 23
  • 24. 升級到SignalR 2(2) • Global.ascx.cs移除RouteTable protected void Application_Start(object sender, EventArgs e) { RouteTable.Routes.MapHubs(); } • 新增Owin startup 24
  • 25. 升級到SignalR 2(3) • Startup.cs填入以下code using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(SignalRChat.Startup))] namespace SignalRChat { public class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } } • Hub.cs 增加Using using Microsoft.AspNet.SignalR; 25
  • 26. [Performance Tuning](1) https://github.com/SignalR/SignalR/wiki/Performance • IIS Configuration Max concurrent requests per applicationm: 預設5000提升到100,000 cd %windir%System32inetsrv appcmd.exe set config /section:system.webserver/serverRuntime /appConcurrentRequestLimit:100000 26
  • 27. [Performance Tuning](2) • ASP.NET Configuration XmaxConCurrentRequestPerCPU: default 5000, 可以提到20000 打開%windir%Microsoft.NETFrameworkv4.0.30319aspnet.config (Framework64 for 64 bit processes) <?xml version="1.0" encoding="UTF-8" ?> <configuration> <runtime> <legacyUnhandledExceptionPolicy enabled="false" /> <legacyImpersonationPolicy enabled="true"/> <alwaysFlowImpersonationPolicy enabled="false"/> <SymbolReadingPolicy enabled="1" /> <shadowCopyVerifyByTimestamp enabled="true"/> </runtime> <startup useLegacyV2RuntimeActivationPolicy="true" /> <system.web> <applicationPool maxConcurrentRequestsPerCPU="20000" /> </system.web> </configuration> Request Qeue Limit: 可以提到250000 打開%windir%Microsoft.NETFrameworkv4.0.30319Configmachine.config <processModel autoConfig="false" requestQueueLimit="250000" /> 27
  • 28. [Performance Tuning](3) • 減少訊息頻率 • 減低訊息大小:使用JasonIgnore using Newtonsoft.Json; using System; public class ShapeModel { [JsonProperty("l")] public double Left { get; set; } [JsonProperty("t")] public double Top { get; set; } // We don't want the client to get the "LastUpdatedBy" property [JsonIgnore] public string LastUpdatedBy { get; set; } } 28
  • 29. [Performance Tuning](4) • SignalR configuration setting DefaultMessageBufferSize :1000, 如果訊息過大, 可調降該值, ex:500 public class Startup { public void Configuration(IAppBuilder app) { // Any connection or hub wire up and configuration should go here GlobalHost.Configuration.DefaultMessageBufferSize = 500; app.MapSignalR(); } } 29
  • 30. [Performance Tuning](5) • 測試SignalR負載工具-Cran https://github.com/SignalR/SignalR/tree/dev/src/Microsoft.AspNet.S ignalR.Crank • SignalR performance counter 管理Nuget套件, 搜尋signalr.utils 30
  • 31. [Performance Tuning](6) 路徑:專案路經/packages/Microsoft.AspNet.SignalR.Utils.<version>/tools 安裝> SignalR.exe ipc 反安裝> SignalR.exe upc Windows 計數器> perfmon 31
  • 33. 擴充(Scaling out) • Sqlserver • Redius • Azure survice bus http://www.asp.net/signalr/overview/signalr-20/performance-and-scaling/scaleout-in-signalr 33
  • 34. Signal R 2教學資源 http://www.asp.net/signalr/overview/signalr-20 34
  • 35. 35

Notas do Editor

  1. signalR 2003 一月 release