Question

I've the following Hub:

public class ImageHub : Hub {
    public void LoadTheImage(String id, String imageUrl) {
        Clients.All.loadReadyImage(id, imageUrl);
    }
}

And the following JavaScript code on the client side.

$(function () {
    var loadImage = $.connection.imageHub;
    loadImage.client.loadReadyImage = function (id, imageUrl) {
    alert(id + " is ready. Url: " + imageUrl); //this will be much more than an alert.
    };
});

And got the following code on .ashx.cs file, which is a generic handler:

public void ProcessRequest(HttpContext context) {
    //do some stuff here
    var imageContext = GlobalHost.ConnectionManager.GetHubContext<ImageHub>();
    imageContext.Clients.All.loadReadyImage("imageOne", imageUrl);
}

The whole this is intended to call a client side code when Generic Handler completes its process. But it is not working. After the Handler finishes its work, the alert is not showing on the browser.

But when I change the client side code to the following, the alert is showing up.

$(function () {
    var loadImage = $.connection.imageHub;
    loadImage.client.loadReadyImage = function (id, imageUrl) {
        alert(id + " is ready. Url: " + imageUrl);
    };
    $.connection.hub.start().done(function () {
        loadImage.server.loadTheImage('my_id', 'image_url');
    });
});

However, I need to call the function not from the client side, but need to call client side function from server side.

How can I do this?

Thanks everybody.

Was it helpful?

Solution

You have to to initiate the connection, to be able to send and receive messages, as you do it in your second snippet.

$.connection.hub.start()
    .done(function(){ console.log('Now connected, connection ID=' + $.connection.hub.id); })
    .fail(function(){ console.log('Could not Connect!'); });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top