简体   繁体   中英

Can you use SignalR without using Knockout?

All I need is to update a page with alerts when someone else creates one. Wouldn't have to be real time, but every 10 seconds or so minimum. It also needs to flash the tab when something is new is displayed for the user to know something was updated if they are currently on a different page. Thank you.

I've never tried to use knockout with signalR, this is how we do it with near-0 latency.

In our masterpage we include the jquery signalR library

Underneath, we tell it to include the signalR auto-generated script

our Hub class looks like so.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Hubs;

    namespace CHSI.Shared.APIs
    {

    [HubName("applicationMessageHub")]
    public class ApplicationMessageHub : Hub
    {


        private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(5000);
        private IList< DSIDGroup> DSIDGroups = new List<DSIDGroup>();
        public ApplicationMessageHub() 
        {


        }
        public void CheckMessages(object state)
        {
            GetMessages();

        }

        public Models.ApplicationMessage GetCurrentMessage(int DSID)
        {
            Models.ApplicationMessage currentMessage = null;


            return currentMessage;
        }

        public override Task OnConnected()
        {
            string DSID = Context.QueryString["DSID"];
            if (!string.IsNullOrEmpty(DSID))
            {
                Groups.Add(Context.ConnectionId, DSID);

                DSIDGroup currentGroup = (from g in this.DSIDGroups where g.DSID == DSID select g).FirstOrDefault();
                if (currentGroup != null)
                {

                    currentGroup.ConnecedIDs.Add(Context.ConnectionId);
                }
                else
                {
                    currentGroup = new DSIDGroup();
                    currentGroup.DSID = DSID;

                    currentGroup.ConnecedIDs.Add(Context.ConnectionId);
                    this.DSIDGroups.Add(currentGroup);
                }
            }

            return base.OnConnected();
        }

        public override Task OnDisconnected(bool stopCalled)
        {
            foreach (var DSIDgroup in DSIDGroups)
            {
                if (DSIDgroup.ConnecedIDs.Contains(Context.ConnectionId))
                {
                    DSIDgroup.ConnecedIDs.Remove(Context.ConnectionId);
                    if (DSIDgroup.ConnecedIDs.Count == 0)
                    {
                        DSIDGroups.Remove(DSIDgroup);
                    }

                    break;
                }

            }


            return base.OnDisconnected(stopCalled);
        }

        public void BroadcastMessage(Models.ApplicationMessage message)
        {

            Clients.All.SendMessage(message);
        }

        public void clearCache(int DSID)
        {
            Clients.Group(DSID.ToString()).clearCache();
        }
        public Models.ApplicationMessage GetMessages()
        {

            foreach (var group in this.DSIDGroups)
            {
                Models.ApplicationMessage currentMessage = GetCurrentMessage(Convert.ToInt32(group.DSID));
                if (currentMessage != null)
                {
                    Clients.Group(group.DSID).SendMessage(currentMessage);
                }

            }
            return null;
            //return _applicationMessage.GetCurrentMessage();
        }

    }
    public class DSIDGroup
{
    public string DSID {get;set;}

    public IList<string> ConnecedIDs { get;set; }
    public DSIDGroup()
    {
        this.ConnecedIDs = new List<string>();
    }
}
}

This class handles grouping my users into groups based on their account (DSID), but you could group users by chat room, not at all, or some other methodology.

We also call javascript functions elsewhere in the codebase like so.

  var context = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<CHSI.Shared.APIs.ApplicationMessageHub>();
            List<string> dsids = new List<string>();
            dsids.Add(CHSI.Shared.Models.ConnectionManager.Current().CurrentDSID.Value.ToString());
            context.Clients.Groups(dsids).clearCache();

There is a javascript function called SendMessage and another called clearCache which handles those calls. They're defined like so.

            applicationMessageHub.client.sendMessage = function (message) {
                alert(message.SummaryMessage);
            };
            applicationMessageHub.client.clearCache = function () {  
                localStorage.removeItem("companiesCache");
                Class.loadPage(Class);                
            }

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM