2011-09-05 93 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(); 
});