2011-09-05 73 views
4

我有一个按钮,用户可以点击某个按钮来出价。每次投标,将最新的投标广播给其他客户。这就是我使用SignalR的原因。从SignalR Hub类中重定向用户

现在,用户需要有积分,如果他没有积分,我想将他重定向到某处。

更明显的方法使我失败,所以任何建议都是值得欢迎的。

//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类中。

回答

11

服务器:

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