You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SignalR v1.1.2无法捕获WebSocket 1006断开事件求助

Fixing SignalR v1.1.2 Disconnection (Error 1006) & Proper Event Handling

Hey there, let's dig into this SignalR v1.1.2 disconnection headache you're dealing with. The key issue here is that SignalR v1.x uses a completely different API for connection lifecycle events compared to newer versions, which is why all those .on() bindings and mixed property paths weren't working. Let's break down the correct approach.

Why Your Previous Attempts Failed

Most of your code tried using .on('Disconnected', ...) or targeting nested connection.connection properties incorrectly. Here's the breakdown:

  • .on() is only for subscribing to custom server-side Hub events, not built-in connection lifecycle events.
  • In v1.1.2, lifecycle events are directly bound to the root connection object (either $.connection.hub or your hub's connection property) using dedicated methods like .disconnected(), not via event subscription.
  • Some of your syntax had typos or incomplete callbacks (like the first line cut off), which would prevent execution.

Correct Event Binding for v1.1.2

First, confirm your connection setup: if you're using a Hub, your root connection is $.connection.hub (or yourHub.connection). Use this object to bind the lifecycle events before calling .start().

Here's a complete, working example tailored to your scenario:

// Initialize your Hub connection (adjust the hub name to match yours)
var myHub = $.connection.myHub;
var signalRConnection = $.connection.hub;

// 1. Capture disconnections (including the 1006 WebSocket error)
signalRConnection.disconnected(function(error) {
    console.log("Connection disconnected:", error);
    
    // Check for the 1006 error specifically
    if (error && error.message.includes("1006")) {
        console.log("WebSocket closed unexpectedly — initiating reconnect...");
        
        // Option 1: Auto-reconnect after a short delay
        setTimeout(function() {
            signalRConnection.start()
                .done(function() {
                    console.log("Reconnected successfully!");
                    // Add logic here to hide retry modals or notify users
                })
                .fail(function(reconnectError) {
                    console.log("Reconnect failed:", reconnectError);
                    // Option 2: Show a modal for manual user retry
                    // $('#retryConnectionModal').modal('show');
                });
        }, 3000); // Wait 3 seconds before retrying
    }
});

// 2. Capture slow connection warnings
signalRConnection.connectionSlow(function() {
    console.log("Connection is slowing down — check your network.");
    // Notify users of potential connectivity issues
});

// 3. Optional: Track reconnect attempts
signalRConnection.reconnecting(function() {
    console.log("Attempting to reconnect to the server...");
});

// 4. Optional: Confirm successful reconnection
signalRConnection.reconnected(function() {
    console.log("Successfully reconnected!");
});

// Start the connection (make sure events are bound BEFORE this!)
signalRConnection.start()
    .done(function() {
        console.log("Initial connection started successfully.");
    })
    .fail(function(startError) {
        console.log("Failed to start initial connection:", startError);
    });

Key Notes

  • Bind events before .start(): If you bind events after starting the connection, you might miss the initial disconnection event trigger.
  • Error handling: The disconnected callback's error parameter may contain details about the 1006 error (depending on your exact v1.1.2 build) — adjust the check to match what you see in your console.
  • Avoid infinite retries: Add a retry counter to stop attempting reconnections after a few failures (e.g., give up after 5 tries and prompt the user to refresh).

内容的提问来源于stack exchange,提问作者Emre Çamaşuvi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:04:20