Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting a user from within a SignalR Hub class

I have a button that users can click to bid on something. Each bid, broadcasts the latest bid to every other client. That's the reason why I'm using SignalR.

Now, the user needs to have active credits, and if he doesn't have credits I want to redirect him somewhere.

The more obvious approach fails me, so any suggestion is welcome.

//Does the user have credits to spend?
if (user.LanceCreditBalance >= 1)
{
    //populate the "ar" object and send it out to everybody.
    var result = Json.Encode(ar);
    Clients.addMessage(result);
}
else
{
    //And this isn't working as expected. Doesn't redirect
    //And causes a 302 error when viewing the Firebug console in Firefox.
    HttpContext.Current.Response.Redirect(@"http://www.google.com");
}

The above code is all within the Chat class which inherits from the SignalR.Hub class.

like image 436
Only Bolivian Here Avatar asked Sep 05 '11 17:09

Only Bolivian Here


1 Answers

Server:

if(user.LanceCreditBalance >= 1)
{
    var result = Json.Encode(ar);
    // send Message to all clients
    Clients.addMessage(result);
}
else
{
    // Invoke a js-Function only on the current client
    Caller.redirectMe("http://www.google.com");
}

Client:

$(function () {
    var chat = $.connection.chat;

    chat.addMessage = function(message) {
        // do something
    };

    // function the server can invoke
    chat.redirectMe = function(target) {
        window.location = target;
    };

    $.connection.hub.start();
});
like image 174
Simser Avatar answered Oct 17 '22 04:10

Simser