【问题标题】:Redirecting a user from within a SignalR Hub class从 SignalR Hub 类中重定向用户
【发布时间】:2011-09-05 17:50:39
【问题描述】:

我有一个按钮,用户可以点击该按钮对某项内容进行出价。每次出价,都会向其他所有客户广播最新出价。这就是我使用 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 类中。

【问题讨论】:

    标签: c# asp.net-mvc-3 redirect signalr


    【解决方案1】:

    服务器:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多