【问题标题】:SignalR client does not receive messages after a reconnectionSignalR 客户端在重新连接后未收到消息
【发布时间】:2017-05-26 09:53:41
【问题描述】:

我正在使用带有 javascript 客户端的 signalR 为 Web 应用程序后端中长时间运行的 powershell 任务提供进度条。在任务启动后,它会导致应用程序池回收(当调用 powershell 时会发生这种情况,我似乎无法解决这个问题),signalR 执行重新连接,这看起来成功,但进度消息停止出现通过。如果我调试路径,单步执行代码,消息仍然从 powershell 发出,并且广播被调用,但客户端没有收到任何东西。

启动:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}

枢纽:

public class ProgressHub : Hub
{
    public void SendProgress(string progressMessage, int progressCount)
    {
        var hubContext = GlobalHost.ConnectionManager.GetHubContext<ProgressHub>();

        hubContext.Clients.All.AddProgress(progressMessage, progressCount);
    }
}

客户:

var progress = $.connection.progressHub;
console.log(progress);

progress.client.addProgress = function (message, percentage) {

    $('#actionDetail').html(message);

    $("#progressBar").progressbar({
        value: percentage
    });             
};

$.connection.hub.logging = true;
$.connection.hub.start().done(function () {

    var connectionId = $.connection.hub.id;
    console.log(connectionId);
});

客户日志:

[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: serverSentEvents transport starting.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://2012r2-en-09:7750/signalr/connect?transport=serverSentEvents&clientP…BoA%3D%3D&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=10'.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: EventSource connected.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: serverSentEvents transport connected. Initiating start request.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: The start request succeeded. Transitioning to the connected state.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:40 GMT+0000 (GMT Standard Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
VM145:1 718e2f95-4950-4350-8dff-4b7cb9f1158a
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:46 GMT+0000 (GMT Standard Time)] SignalR: EventSource readyState: 0.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:46 GMT+0000 (GMT Standard Time)] SignalR: EventSource reconnecting due to the server connection ending.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:48 GMT+0000 (GMT Standard Time)] SignalR: EventSource calling close().
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:48 GMT+0000 (GMT Standard Time)] SignalR: serverSentEvents reconnecting.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:48 GMT+0000 (GMT Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://2012r2-en-09:7750/signalr/reconnect?transport=serverSentEvents&messa…SBoA%3D%3D&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=7'.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:50 GMT+0000 (GMT Standard Time)] SignalR: EventSource connected.
VM145:1 718e2f95-4950-4350-8dff-4b7cb9f1158a
5jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:07:59 GMT+0000 (GMT Standard Time)] SignalR: Triggering client hub event 'AddProgress' on hub 'ProgressHub'.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:08:06 GMT+0000 (GMT Standard Time)] SignalR: EventSource readyState: 0.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:08:06 GMT+0000 (GMT Standard Time)] SignalR: EventSource reconnecting due to the server connection ending.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:08:08 GMT+0000 (GMT Standard Time)] SignalR: EventSource calling close().
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:08:08 GMT+0000 (GMT Standard Time)] SignalR: serverSentEvents reconnecting.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:08:08 GMT+0000 (GMT Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://2012r2-en-09:7750/signalr/reconnect?transport=serverSentEvents&messa…SBoA%3D%3D&connectionData=%5B%7B%22name%22%3A%22progresshub%22%7D%5D&tid=9'.
jquery?v=WM6erXjzG36po83czfGO2Wf5wbUN0yj0TfE47rwndb41:1 
[10:08:10 GMT+0000 (GMT Standard Time)] SignalR: EventSource connected.
VM145:1 718e2f95-4950-4350-8dff-4b7cb9f1158a

从日志中,您可以看到在初始连接后,执行了重新连接,在此之后消息仍然继续通过。在应用程序池被回收的第二个之后,它们停止了。

那里有类似的问题,例如这个:SignalR client is reconnected after Owin restart, but message is not published 但是,尝试提到的解决方案,他甚至在重新连接之前就停止了所有消息。

【问题讨论】:

  • 从你的代码我看不到你在哪里打电话SendProgress
  • SendProgress 是从控制器类中调用的,一个委托回调被传递到后端方法中,该方法回调到控制器,进而调用SendProgress。如果我逐步执行代码,这将按预期工作,在应用程序池回收后,我们仍然调用 SendProgress,但客户端没有收到消息。
  • 您是否调试到 ser 停止的位置或未调用的 ser?
  • 你的中心方法SendProgress中的代码不需要调用GetHubContext。只需替换为:Clients.All.AddProgress(progressMessage, progressCount);
  • 如果这样做,我会收到错误消息“不支持使用不是由 HubPipeline 创建的 Hub 实例。”

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


【解决方案1】:

当 SignalR 断开连接并通过完全重新创建 signalR 集线器对象解决客户端问题时,我们遇到了同样的问题:

// SignalR disconnected callback:
$.connection.hub.disconnected(() => {
    setTimeout(() => {
        // Reset the SignalR hub and recreate the hub proxies:
        $.signalR.hub = null;
        $.signalR.hub = $.hubConnection('/signalr', { useDefaultPath: false });
        $.extend($.signalR, $.signalR.hub.createHubProxies());

        // Re-subscribe to your events:
        $.connection.myHub.client.onMessage = message => {
            console.log(`onMessage[MyHub]: ${message}`);
        };
        
        // SignalR restart:
        $.connection.hub.start();
    }, 5000);
});

我们在日志中看到我们的onMessage 函数在断开连接(并且应该重新连接)后没有被触发,因此重新创建集线器连接并在再次连接之前调用createHubProxies() 有效。我们通过超时连接(在服务器代码中的某处添加断点)在本地对此进行了测试,该连接触发了disconnected() 函数。

【讨论】:

    【解决方案2】:
    1. 请求任务后,应将任务 ID 返回给客户端。
    2. 将任务 ID 存储在客户端(本地存储或 cookie)的某个位置,重新连接时,您必须订阅该任务 ID 的进度。

    ps:即使用户不小心重新加载页面,您也需要此功能。

    【讨论】:

      猜你喜欢
      • 2016-04-29
      • 2013-05-21
      • 1970-01-01
      • 2020-08-13
      • 2016-08-25
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多