简体   繁体   中英

SignalR version 1.2.2 client side method not being called, nor is console logging being made

I am using this tutorial: https://docs.microsoft.com/en-us/aspnet/signalr/overview/older-versions/tutorial-server-broadcast-with-aspnet-signalr to broadcast a message from Conext stored in singleton. I have a few issues.

First, there doesn't seem to be any issues from assigning the hub and making a connection from the client side. This is my code:

  $(function () {
      var transactionHub = $.connection.TransactPtHub; // the generated client-side hub proxy

      transactionHub.client.broadcastDmrUpdate = function (test) {

          alert("Yo-Yo Ma!!" + test);
      };
      console.log('test');
      $.connection.hub.logging = true;
      $.connection.hub.start()
          .done(function () { console.log('Now connected, connection ID=' + $.connection.hub.id); })
          .fail(function () { console.log('Could not Connect!'); });

      if ($.connection.hub.state === $.signalR.connectionState.disconnected) {
          alert("connected");
      }
      else {
          alert("not connected");
      }

  });

My alert, "connected" does display on page load. The function in this part of the code, "transactionHub.client.broadcastDmrUpdate = function (test) {..." never gets called from the server side.

This is my singleton:

public class TransactPtSingleton
{
    private readonly static Lazy<TransactPtSingleton> _instance = new Lazy<TransactPtSingleton>(() => new TransactPtSingleton(GlobalHost.ConnectionManager.GetHubContext<TransactPtHub>().Clients));

    private TransactPtSingleton(IHubConnectionContext clients)
    {
        Clients = clients;
    }

    private IHubConnectionContext Clients
    {
        get;
        set;
    }

    public static TransactPtSingleton Instance
    {
        get
        {
            return _instance.Value;
        }
    }

    public void BroadcastDmrUpdate(string dmr)
    {
        Clients.All.broadcastDmrUpdate(dmr);
    }  
}

and this is my hub,

[HubName("TransactPtHub")]
public class TransactPtHub : Hub
{
    public void UpdateDailyTransactionTable()
    {

    }
}

So, I don't get a call back to the client function on broadcast, and even though I set my logging to true, I am not seeing any logs inside of my browser console. Where do I begin to troubleshoot? Or what am I doing wrong? Thank you.

UPDATE: I misread my own diagnostics.
if ($.connection.hub.state === $.signalR.connectionState.disconnected) {

is triggering, so the connection is not starting for some reason. Also, the .done and .fail never get entered, and I never get any console messages.

Update 2: Combing through the SignalR.js it seems to have something to do with,

        // Check to see if start is being called prior to page load
        // If waitForPageLoad is true we then want to re-direct function call to the window load event
        if (!_pageLoaded && config.waitForPageLoad === true) {
            connection._.deferredStartHandler = function () {
                connection.start(options, callback);
            };
            _pageWindow.bind("load", connection._.deferredStartHandler);

            return deferred.promise();
        }

as on return deferred.promise() it leaves the SignalR.js and comes back to the calling script in UI.

Figured it out. So this basically stems back to my original question here: Question Which initially stemmed from using SignalR 1.2.2 with JQuery version 3

From my "Update 2"

 if (!_pageLoaded && config.waitForPageLoad === true) {....

That are was having issues because with the deprecation of .load in Jquery > 3 I needed to change .load to .on as per:

Breaking change: .load(), .unload(), and .error() removed

These methods are shortcuts for event operations, but had several API limitations. The event .load() method conflicted with the ajax .load() method. The .error() method could not be used with window.onerror because of the way the DOM method is defined. If you need to attach events by these names, use the .on() method, eg change $("img").load(fn) to $("img").on("load", fn).

From: JQuery Guide

But I goofed up and inside jquery.signalR-1.2.2js, instead of changing,

_pageWindow.load(function () { _pageLoaded = true; });

To

_pageWindow.on("load", function () { _pageLoaded = true; });  

I did,

_pageWindow.on(function () { _pageLoaded = true; });

No console errors were being displayed so it made things tougher. But, SignalR connection now is established from the client side, and also SignalR logging is now working.

So if you are using an older version of SignalR with a newer version of JQuery, this may be your problem and solution.

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