从信号集线器类中重定向用户
-
26-10-2019 - |
题
我有一个按钮,用户可以单击以竞标某物。每次出价,向其他每个客户端广播了最新的出价。这就是我使用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类继承。
解决方案
服务器:
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();
});
不隶属于 StackOverflow