Question

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.

Was it helpful?

Solution

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();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top