Pergunta

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?

Foi útil?

Solução

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.com/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);

Outras dicas

You need to read the docs. Everything in your sample looks wrong. Start here:

https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top