SignalR使用配置
-
利用nuget管理器添加组件包Install-Package Microsoft.AspNet.SignalR -Version 2.1.0 // (-Version 2.1.0可有可无,没有就默认最新版本)添加成功后:会在引用里面多了些引用,Scripts文件夹下有要使用到的js文件,还有一个packages.config文件.
1 <packages> 2 <package id="jQuery" version="1.6.4" targetFramework="net40" /> 3 <package id="Microsoft.AspNet.SignalR" version="2.1.0" targetFramework="net40" /> 4 <package id="Microsoft.AspNet.SignalR.Client" version="2.1.0" targetFramework="net40" /> 5 <package id="Microsoft.AspNet.SignalR.Core" version="2.1.0" targetFramework="net40" /> 6 <package id="Microsoft.AspNet.SignalR.JS" version="2.1.0" targetFramework="net40" /> 7 <package id="Microsoft.AspNet.SignalR.Owin" version="1.2.1" targetFramework="net40" /> 8 <package id="Microsoft.AspNet.SignalR.SystemWeb" version="2.1.0" targetFramework="net40" /> 9 <package id="Microsoft.Owin" version="2.0.1" targetFramework="net40" /> 10 <package id="Microsoft.Owin.Host.SystemWeb" version="2.0.1" targetFramework="net40" /> 11 <package id="Microsoft.Owin.Security" version="2.0.1" targetFramework="net40" /> 12 <package id="Newtonsoft.Json" version="5.0.1" targetFramework="net40" /> 13 <package id="Owin" version="1.0" targetFramework="net40" /> 14 </packages>
注:这里的web.config配置版本很重要。
1. NET Framework 4.0
For the .NET client - 2.0.2 http://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/2.0.2
PM> Install-Package Microsoft.AspNet.SignalR.Client -Version 2.0.2
For the Server - 1.2.0 http://www.nuget.org/packages/Microsoft.AspNet.SignalR/1.2.0
PM> Install-Package Microsoft.AspNet.SignalR -Version 1.2.02. NET Framework 4.5
For the .NET client - 2.1.0 https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/2.1.0
PM> Install-Package Microsoft.AspNet.SignalR.Client
For the Server - 2.1.0 https://www.nuget.org/packages/Microsoft.AspNet.SignalR/2.1.0
PM> Install-Package Microsoft.AspNet.SignalR -
1 <system.web> 2 <compilation debug="true" targetFramework="4.5" /> 3 <httpRuntime targetFramework="4.5" /> 4 </system.web> 5 <system.webServer> 6 <validation validateIntegratedModeConfiguration="false" /> 7 <modules runAllManagedModulesForAllRequests="true"> 8 </modules> 9 </system.webServer>
- 创建路由类:Startup.cs
[assembly: OwinStartup(typeof(Demo.Startup))] namespace Demo { public class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); SignalR 2.1.0 // app.MapHubs(); SignalR 1.2.0 } } }
或者 在Global.asax.cs的Application_Start里面添加 RouteTable.Routes.MapHubs();
-
在js里面引用后台Hub的类时,启用的是骆驼命名法,所以类名的首字母得小写.
//如: var noticeHub = $.connection.noticeHub; /*
对应的是 后台NoticeHub.cs类; 也可以重命名类名,在类名上方加上[HubName("NoticeHub")], 那么前台就如下调用: */ var noticeHub = $.connection.NoticeHub; -
在后台类里面,通过 var context = GlobalHost.ConnectionManager.GetHubContext<MessengerHub>();
得到Hub对象。 -
添加引用
<script type="text/javascript" src="Scripts/jquery-1.6.4.js"></script>
<script type="text/javascript" src="Scripts/jquery.signalR-1.2.0.js"></script>
引用Hub只能放最后,如果发布之后提示找不到/Signalr/Hubs,也可以现在本地运行正常的时候,去站点/Signalr/Hubs把内容复制起来存储到新建的Hub.js里面,引用Hub.js也是一样的效果。
<%--<script type="text/javascript" src="/Signalr/Hubs"></script>--%>
<script src="Scripts/Hub.js" type="text/javascript"></script> - client端代码:
1 <script> 2 $(function () { 3 var noticeHub = $.connection.noticeHub; 4 noticeHub.client.sendMsg = function (msg) { alert(msg); }; 5 6 $.connection.hub.start().done(function () { 7 $("#spText").html("功能初始化完成!"); 8 }); 9 或者 //$.connection.hub.start(); 10 11 }); 12 </script> 13 server端代码: 14 <script> 15 $(function () { 16 var noticeHub = $.connection.noticeHub; 17 $.connection.hub.start(); 18 19 $("#btnSend").click(function () { 20 var msg = $("#txtMsg").val(); 21 noticeHub.server.sendNotice(msg); 22 }); 23 }); 24 </script>
7.跨域调用:
var connection = new HubConnection("http://localhost:14251/"); SignalRConsole.SignalR.MessengerHub.Message message = new SignalR.MessengerHub.Message(); message.Content = "Moo"; message.Duration = 500; message.Title = "Hello Title"; var myHub = connection.CreateHubProxy("messenger"); connection.Start().Wait(); Object[] myData = { (Object)message, "sourcing" }; myHub.Invoke("broadCastMessage", myData); connection.Dispose(); //这句很重要,不然容易宕掉