English 中文(简体)
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.

最佳回答

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();
});
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签