Example of SignalR active notification subscriber and signalr subscription

Source: Internet
Author: User

Example of SignalR active notification subscriber and signalr subscription

Html code:

<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 code:

[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();        }    }

Call the Notify method.

Key Point: GlobalHost. ConnectionManager. GetHubContext

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.