admin管理员组

文章数量:1336632

I have the following hub in my MVC application from where I would like to send a simple message to my client side code:

using SignalR.Hubs;  
    
public  class Progress : Hub
{
       public void Send(string message)
       {
            // Call the addMessage method on all clients
            Clients.addMessage(message);
       }
          
        public Progress()
        {
            Clients.addMessage("Starting to analyze image");
        }                 
}

And the following javascript in my view

<script src="/Scripts/jquery.signalR.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {

        // Proxy created on the fly
        var connection = $.connection('/signalr/hubs/progress');

        // Declare a function on the chat hub so the server can invoke it
        connection.addMessage = function (message) {
            $('#messages').append('<li>' + message.Content + '</li>');
        };


        // Start the connection
        connection.start();
    });
</script>

My problem is when the code calls the constructor, or the Send method for that matter, the Clients object is null.

Everything looks OK when I debug the client side code. The /signalr/hubs/ route returns javascript code and there are no errors when the javascript is run.

I can add that the backend code runs on top of the Umbraco 5 CMS environment which I am not sure is causing any disturbances.

Any suggestions on how I can debug/solve this?

I have the following hub in my MVC application from where I would like to send a simple message to my client side code:

using SignalR.Hubs;  
    
public  class Progress : Hub
{
       public void Send(string message)
       {
            // Call the addMessage method on all clients
            Clients.addMessage(message);
       }
          
        public Progress()
        {
            Clients.addMessage("Starting to analyze image");
        }                 
}

And the following javascript in my view

<script src="/Scripts/jquery.signalR.js" type="text/javascript"></script>
<script src="/signalr/hubs" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {

        // Proxy created on the fly
        var connection = $.connection('/signalr/hubs/progress');

        // Declare a function on the chat hub so the server can invoke it
        connection.addMessage = function (message) {
            $('#messages').append('<li>' + message.Content + '</li>');
        };


        // Start the connection
        connection.start();
    });
</script>

My problem is when the code calls the constructor, or the Send method for that matter, the Clients object is null.

Everything looks OK when I debug the client side code. The /signalr/hubs/ route returns javascript code and there are no errors when the javascript is run.

I can add that the backend code runs on top of the Umbraco 5 CMS environment which I am not sure is causing any disturbances.

Any suggestions on how I can debug/solve this?

Share Improve this question edited Dec 30, 2020 at 13:38 Serlok 4621 gold badge11 silver badges25 bronze badges asked Feb 19, 2012 at 16:19 Kristian Højklint SchneiderKristian Højklint Schneider 931 silver badge7 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 6

It sounds like you are trying to broadcast a message from server side code by instantiating the hub. Unfortunately it doesn't work like that. You can see an example of how to send messages from the server side here: https://github./SignalR/SignalR/wiki/Hubs. Take a look at the "Broadcasting over a Hub from outside of a Hub" section.

The following would be used on the server side where you want to do the broadcast from

using SignalR.Infrastructure;

string message = "Test Message";
IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<MyHub>();
clients.addMessage(message);

This matches your Send() method however if you are trying to setup a progress indicator you probably only want to send messages to the caller. In this case you need to update the Progress method to Caller.addMessage("Starting to analyze image");. To do this from outside of the hub is a little more tricky as you will need to keep track of the client Id for the connection you want to update. Once you know that the above changes to:

clients[clientId].addMessage(message);

本文标签: javascriptClients is null when trying to send message via signalRStack Overflow