.NET 4.0下使用 SignalR

来源:互联网 发布:淘客程序源码一键部署 编辑:程序博客网 时间:2024/06/06 13:23

项目:ASP.NET MVC 4 Web 应用程序

开发环境:VS2012

目标框架:.NET Framework 4

 

SignalR 主要是用于消息推送的一个框架

SignalR是什么 http://www.cnblogs.com/humble/p/3850749.html

最好的入门文章 应该是 http://www.asp.net/signalr/overview/getting-started/introduction-to-signalr  简单明了

安装时遇到的 http://www.cnblogs.com/ding2011/p/3468740.html

 

 

 

 

 

安装

工具 -> 库程序包管理器 -> 程序包管理器控制台

输入下面命令 install-package Microsoft.AspNet.SignalR -Version 1.1.4

因为是 .net 4.0 用不了 SignalR 2以上的版本

安装完毕发现项目多了一个Script 文件夹

主要用到 jquery-1.6.4.min.js和jquery.signalR-1.1.4.min.js,其他可以删除

 

 

 

 

 

BaseController.cs

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. //工具 -> 库程序包管理器 -> 程序包管理器控制台 输入下面命令  
  8. //install-package Microsoft.AspNet.SignalR -Version 1.1.4  
  9. using Microsoft.AspNet.SignalR;  
  10.   
  11. namespace SignalR.Controllers  
  12. {  
  13.   
  14.     //所有 hub  
  15.     public class AllHub : Hub  
  16.     {  
  17.   
  18.     }  
  19.   
  20.     //当前 hub  
  21.     public class CurHub : Hub  
  22.     {  
  23.         public void SetRecGroup(string id)//设置接收组  
  24.         {  
  25.             this.Groups.Add(this.Context.ConnectionId, id);  
  26.         }  
  27.     }  
  28.   
  29.     public class BaseController : Controller  
  30.     {  
  31.   
  32.         public ActionResult ProgressBar()  
  33.         {  
  34.             return View();  
  35.         }  
  36.   
  37.         public ActionResult Broadcast()  
  38.         {  
  39.             return View();  
  40.         }  
  41.   
  42.         //进度条  
  43.          public void fnProgressBar()  
  44.         {  
  45.             for (int i = 0; i < 100; i++)  
  46.             {  
  47.                 IHubContext chat = GlobalHost.ConnectionManager.GetHubContext<CurHub>();  
  48.                 chat.Clients.Group("clientId").notify(i);//向指定组发送  
  49.                 System.Threading.Thread.Sleep(100);  
  50.             }  
  51.         }  
  52.   
  53.         //广播  
  54.          public string fnBroadcast(string msg)  
  55.         {  
  56.             string result = "发送失败!";  
  57.             try  
  58.             {  
  59.                 IHubContext chat = GlobalHost.ConnectionManager.GetHubContext<AllHub>();  
  60.                 chat.Clients.All.notice(msg);//向所有组发送  
  61.                 result = "发送成功!";  
  62.             }  
  63.             catch (Exception e)  
  64.             {  
  65.                 result = "发送失败!\n失败原因:\n" + e.Message;  
  66.             }  
  67.             return result;  
  68.         }  
  69.   
  70.     }  
  71. }  


 

 

 

 

Global.asax.cs

[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Http;  
  6. using System.Web.Mvc;  
  7. using System.Web.Routing;  
  8.   
  9. namespace SignalR  
  10. {  
  11.     // Note: For instructions on enabling IIS6 or IIS7 classic mode,   
  12.     // visit http://go.microsoft.com/?LinkId=9394801  
  13.     public class MvcApplication : System.Web.HttpApplication  
  14.     {  
  15.         protected void Application_Start()  
  16.         {  
  17.             //添加 SignalR 映射  
  18.               //注意执行顺序  放在最后面 会失败  
  19.               //不添加  <script src="~/signalr/hubs"></script> 会生成失败  
  20.               RouteTable.Routes.MapHubs();  
  21.   
  22.             AreaRegistration.RegisterAllAreas();  
  23.   
  24.             WebApiConfig.Register(GlobalConfiguration.Configuration);  
  25.             FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
  26.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  27.         }  
  28.     }  
  29. }  


 

 

 

 

Broadcast.cshtml

[csharp] view plain copy
  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.     <head runat="server">  
  5.         <title>消息推送页面</title>  
  6.         <script src="~/Scripts/jquery-1.6.4.min.js"></script>  
  7.         <script src="~/Scripts/jquery.signalR-1.1.4.min.js"></script>  
  8.         <script src="~/signalr/hubs"></script>  
  9.     </head>  
  10.     <body>  
  11.         <input type="text" id="txtMess"/>  
  12.         <input type="button" value="发送广播消息" id="btnPush"/>  
  13.   
  14.         <script>  
  15.             (function () {  
  16.   
  17.                 //发送信息  
  18.                 $("#btnPush").click(function () {  
  19.                     var $mess = $("#txtMess");  
  20.                     $.ajax({  
  21.                         url: "../Base/fnBroadcast",  
  22.                         data: { msg: $mess.val() },  
  23.                         type: "post",  
  24.                         dataType: "text",  
  25.                         success: function (result) {  
  26.                             if (result.indexOf("成功") !== -1) {  
  27.                                 $mess.val("");  
  28.                             }  
  29.                             else {  
  30.                                 alert(result);  
  31.                             }  
  32.                         }  
  33.                     });  
  34.                 });  
  35.   
  36.                 //接收信息  
  37.                 var allHub = $.connection.allHub;//对应后台的 类 AllHub  
  38.                 allHub.client.notice = function (msg) {  
  39.                     $("body").append("<br/><br/>" + msg);  
  40.                 }  
  41.                 $.connection.hub.start();  
  42.   
  43.             })();  
  44.         </script>  
  45.     </body>  
  46. </html>  


 

 

 

 

ProgressBar.cshtml

[csharp] view plain copy
  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4.     <head runat="server">  
  5.         <title>进度条</title>  
  6.         <script src="~/Scripts/jquery-1.6.4.min.js"></script>  
  7.         <script src="~/Scripts/jquery.signalR-1.1.4.min.js"></script>  
  8.         <script src="~/signalr/hubs"></script>  
  9.     </head>  
  10.     <body>  
  11.         <div id="loading" style="width: 0%;">Loading</div>  
  12.         <input id="submit" type="button" value="Start" />  
  13.   
  14.         <script>  
  15.             (function () {  
  16.   
  17.                 //接收信息  
  18.                 var curHub = $.connection.curHub;//对应后台的类CurHub  
  19.                 curHub.client.notify = function (msg) {  
  20.                     $("#loading").css({ "background-color""blue""width": Number(msg) + "%" });  
  21.                 };  
  22.                 $.connection.hub.start().done(function () {  
  23.                     curHub.server.setRecGroup("clientId");//设置接收组,该方法对应后台的类CurHub的SetRecGroup  
  24.                 });  
  25.   
  26.                 $("#submit").click(function () {  
  27.                     $.get("../Base/fnProgressBar", function () { });  
  28.                 });  
  29.   
  30.             })();  
  31.         </script>  
  32.     </body>  
  33. </html>  

 

 

 

 

注意

<script src="~/signalr/hubs"></script> 这个js文件是动态生成的,项目里面是找不到该文件的

要确保 动态生成成功 必须在 Global.asax.cs 添加 映射 具体查看上面的 Global.asax.cs

0 0
原创粉丝点击