星空网 > 软件开发 > ASP.net

SignalR主动通知订阅者示例

html代码:

<script src='/images/loading.gif' data-original="~/Scripts/jquery.signalR-2.2.0.min.js"></script><script src='/images/loading.gif' data-original="@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




原标题:SignalR主动通知订阅者示例

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

慕尼黑啤酒节:https://www.goluckyvip.com/tag/38845.html
穆萨正通:https://www.goluckyvip.com/tag/38846.html
穆斯林入斋:https://www.goluckyvip.com/tag/38847.html
哪个跨境电商好:https://www.goluckyvip.com/tag/38848.html
新政策:https://www.goluckyvip.com/tag/3885.html
哪个免费外贸网站好:https://www.goluckyvip.com/tag/38851.html
宠物梳专利查询分析:https://www.kjdsnews.com/a/1842293.html
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流