6

我有一个按钮,用户可以单击该按钮对某事进行出价。每次出价,都会向所有其他客户广播最新出价。这就是我使用 SignalR 的原因。

现在,用户需要有活动积分,如果他没有积分,我想将他重定向到某个地方。

更明显的方法让我失望,所以欢迎任何建议。

//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");
}

上面的代码都在继承自 SignalR.Hub 类的 Chat 类中。

4

1 回答 1

12

服务器:

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");
}

客户:

$(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();
});
于 2011-09-13T16:14:24.880 回答