SignalRハブクラス内からユーザーをリダイレクトします
-
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