简体   繁体   中英

SignalR Broadcast from Controller Not Invoking Client Method

I'm using Signalr 1.1 and I'm trying a simple project involving updating a progress bar. I've read other people having similar issues but non seem to really help. I'm thinking I may have lost my mind.

I set up a hub

using Microsoft.AspNet.SignalR;

namespace VBS_MVC.Hubs
{
    public class ProgressBarHub : Hub
    {
        public void CheckSend()
        {
            Clients.All.updateProgressBar();
        }
    }
}

Which, I don't really need for this, but I have it set up for testing.

My controller looks like this:

 [HttpPost]
        public ActionResult ProgressBarTest(string SignalRConnectionId)
        {
            // Get the value of the hidden from the request
            string currentClientConnectionId = SignalRConnectionId;

            // Get the hub context
            Microsoft.AspNet.SignalR.IHubContext myHubContext = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<Hubs.ProgressBarHub>();

            // Resolve a the client that corresponds to the current request
            dynamic currentClient = myHubContext.Clients.Client(currentClientConnectionId);

            currentClient.updateProgressBar();
            System.Threading.Thread.Sleep(2000);

            myHubContext.Clients.Client(currentClientConnectionId).updateProgressBar();
            System.Threading.Thread.Sleep(3000);

            myHubContext.Clients.All.updateProgressBar();
            System.Threading.Thread.Sleep(2000);


            return View();
        }

and on my client:

<h2>ProgressBarTest</h2>
@using (Html.BeginForm())
{

<input type="hidden" id="mySignalRConnectionIdHidden" name="SignalRConnectionId" />
 <input type="submit" />
}

<button class="signalr">Test Send</button>

<div id="counter">
Counter
</div>

 <script src="../../Scripts/jquery.signalR-1.1.4.min.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="/signalr/hubs"></script>
    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript">

        $(function () {

            var progress = $.connection.progressBarHub;

            // Create a function that the hub can call to broadcast messages.
            progress.client.updateProgressBar = function () {
                console.log('test');
            };

            $('.signalr').click(function () {
                progress.server.checkSend();
            });

            $.connection.hub.start().done(function () {
                $("#mySignalRConnectionIdHidden").val($.connection.hub.id);
            });

            $.connection.hub.start();
        });


    </script>

The connectionId looks like its making it into the post just fine, and if I click on the button I have outside of the form (it talks directly to the hub) that works fine. As you can see I'm trying to broadcast to the client in different ways but nothing seems to work. What am I missing?

Try changing this line:

Clients.All.updateProgressBar();

To:

Clients.All.UpdateProgressBar();

I believe the dynamic method names are case sensitive.

Try adding this to your hub class:

[HubName("progressBarHub")]

The hub name is case sensitive and you are calling it with a lowercase 'p' in your javascript.

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