Asp net SignalR 实现服务端消息推送到Web端

上传人:pu****.1 文档编号:505462312 上传时间:2024-02-29 格式:DOCX 页数:18 大小:196.94KB
返回 下载 相关 举报
Asp net SignalR 实现服务端消息推送到Web端_第1页
第1页 / 共18页
Asp net SignalR 实现服务端消息推送到Web端_第2页
第2页 / 共18页
Asp net SignalR 实现服务端消息推送到Web端_第3页
第3页 / 共18页
Asp net SignalR 实现服务端消息推送到Web端_第4页
第4页 / 共18页
Asp net SignalR 实现服务端消息推送到Web端_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《Asp net SignalR 实现服务端消息推送到Web端》由会员分享,可在线阅读,更多相关《Asp net SignalR 实现服务端消息推送到Web端(18页珍藏版)》请在金锄头文库上搜索。

1、A SignalR 实现服务端消息推送到Web端 之前的文章介绍过A SignalR, ASP .NET SignalR是一个ASP .NET 下的类库,可以在ASP .NET 的Web项目中实现实时通信. 今天我们来实现服务端消息推送到Web端, 首先回顾一下它抽象层次图是这样的:实际上 A SignalR 2 实现 服务端消息推送到Web端, 更加简单. 为了获取更好的可伸缩性, 我们引入消息队列, 看如下基本流程图:消息队列MQ监听, 在Web site 服务端一收到消息,马上通过Signalr 推送广播到客户端. 创建ASP.NET MVC WEB APP, 从NuGet 安装Sign

2、alR 2.12Install-Package Microsoft.AspNet.SignalR具体实现代码,是这样的,我们增加一个空的Hub: public class FeedHub : Hub public void Init() 是简单的消息模型, 标题与正文属性: Serializable public class PushMessageModel public int Id get; set; public string MSG_TITLE get; set; public string MSG_CONTENT get; set; 服务端推送具体类,记录日志, 创建消息队列实例,监

3、听, 等待收取消息. 这里我们使用的是AcitveMQ的.net客户端. ActiveMQListenAdapter是一个封装过的对象. public class MQHubsConfig private static ILogger log = new Logger(MQHubsConfig); / / Registers the mq listen and hubs. / public static void RegisterMQListenAndHubs() var activemq = Megadotnet.MessageMQ.Adapter.ActiveMQListenAdapter

4、.Instance(MQConfig.MQIpAddress, MQConfig.QueueDestination); activemq.MQListener += m = log.InfoFormat(从MQ收到消息0, m.MSG_CONTENT); GlobalHost.ConnectionManager.GetHubContext().Clients.All.receive(m); ; activemq.ReceviceListener(); 上面有一句关键代码GlobalHost.ConnectionManager.GetHubContext().Clients.All.receiv

5、e(m); 这里使用了GetHubContext方法后,直接来广播消息.需要在MVCApplication下加载: public class MvcApplication : System.Web.HttpApplication protected void Application_Start() AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleC

6、onfig.RegisterBundles(BundleTable.Bundles); MQHubsConfig.RegisterMQListenAndHubs(); 同时需要增加一个Starup.cs, 用于Owinassembly: OwinStartup(typeof(RealTimeApp.Startup)namespace RealTimeApp public class Startup public void Configuration(IAppBuilder app) / Any connection or hub wire up and configuration should

7、 go here app.MapSignalR(); 接下来是客户端App.js:function App() var init = function () Feed(); $.connection.hub.logging = true; $.connection.hub.start() .done(function() console.log(Connected!); $(document).trigger(Connected); ) .fail(function() console.log(Could not Connect!); ); ; init();Feed.js 具体与Signal

8、R.js通信, 创建名为receive的function, 与服务端对应function Feed() var chat = undefined; var init = function () / Reference the auto-generated proxy for the hub. chat = $.connection.feedHub; / Create a function that the hub can call back to display messages. chat.client.receive = function (item) var selector = ul.

9、feed-list lidata-id= + item.Id + ; if (!($(selector).length 0) $(ul.feed-list).prepend($(.feed-template).render(item); $(ul.feed-list li:gt(3).remove(); $.messager.show( title: Tips, msg: item.MSG_CONTENT, showType: show ); ; / Start the connection. $.connection.hub.start().done(function () chat.server.init(); ); ; init(); 上面的javascript代码与服务端有通信, 具体看如下图: 在Index.cshtml, 我们需要引用SignalR客户端JS, 放置hubs, 这里我们使用了jsrender, easyui.js 来呈现推送的消息.model dynamicsection Scripts Scripts.Render(/Scripts/project.js) $(document).ready(function () var app = new App(); ); Feed ul class=span12 f

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 建筑/环境 > 建筑资料

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号