zoukankan      html  css  js  c++  java
  • SignalR主动通知订阅者示例

    html代码:

    <script src="~/Scripts/jquery.signalR-2.2.0.min.js"></script>
    <script src="@Url.Content("~/signalr/hubs")" type="text/javascript"></script>
    <script>
        $(function () {
            var hub = $.connection.payHub;
            hub.client.waitNotify = function (tran, url) {
                console.log("waitNotify:" + tran + "   " + url);
                if (tran) {
                    location.href = url;
                }
    
            };
            $.connection.hub.start().done(function () {
                console.log("hub done");
            });
        })
    </script>
    

    hub代码:

    [HubName("payHub"), Authorize]
        public class payHub : Hub
        {
            public static Dictionary<Guid, string> userPayHub = new Dictionary<Guid, string>();
            public static void Notify(Guid userId, string redirectUrl)
            {
                if (userPayHub.ContainsKey(userId))
                {
                    Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<Hubs.payHub>()
                       .Clients.Client(Hubs.payHub.userPayHub[userId])
                       .waitNotify(true, redirectUrl);
                }
            }
            public override Task OnConnected()
            {
                var uid = this.Context.User.Identity.GetUserId();
                userPayHub[uid] = this.Context.ConnectionId;
                return base.OnConnected();
            }
            public override Task OnDisconnected(bool stopCalled)
            {
                var uid = this.Context.User.Identity.GetUserId();
                userPayHub.Remove(uid);
                return base.OnDisconnected(stopCalled);
            }
            public override Task OnReconnected()
            {
                var uid = this.Context.User.Identity.GetUserId();
                userPayHub.Remove(uid);
                return base.OnReconnected();
            }
        }
    

    通知调用Notify方法即可。

    关键点:GlobalHost.ConnectionManager.GetHubContext

  • 相关阅读:
    杂谈
    分享一首歌——美丽的万物
    Silverlight有感
    风云的银光志Silverlight4.0教程之打印报表和图形(转)
    IIS 7.0的集成模式和经典模式
    WPF中MVVM模式原理分析与实践(转)
    使用HTTP_X_FORWARDED_FOR获取客户端IP的严重后果
    各大网站架构总结笔记(转)
    做了几天silverlight 小结一下
    Fiddler2介绍
  • 原文地址:https://www.cnblogs.com/calvinK/p/5345511.html
Copyright © 2011-2022 走看看