c# net4.0 signalr webapi即时通信

转载自:https://blog.csdn.net/LOW584710047/article/details/46225511/

 

项目: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


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


 

 

 

 

Global.asax.cs


 
 
  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. namespace SignalR
  9. {
  10. // Note: For instructions on enabling IIS6 or IIS7 classic mode,
  11. // visit http://go.microsoft.com/?LinkId=9394801
  12. public class MvcApplication : System. Web. HttpApplication
  13. {
  14. protected void Application_Start()
  15. {
  16. //添加 SignalR 映射
  17. //注意执行顺序 放在最后面 会失败
  18. //不添加 <script src="~/signalr/hubs"></script> 会生成失败
  19. RouteTable.Routes.MapHubs();
  20. AreaRegistration.RegisterAllAreas();
  21. WebApiConfig.Register(GlobalConfiguration.Configuration);
  22. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  23. RouteConfig.RegisterRoutes(RouteTable.Routes);
  24. }
  25. }
  26. }


 

 

 

 

Broadcast.cshtml


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

ProgressBar.cshtml

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

 

 

 

 

注意

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

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

另外这个只有广播的,下面这个是指定用户的,亲测可用
https://blog.csdn.net/qq_35493664/article/details/77620305
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值