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

Atmosphere框架WebSocket 302状态码处理方案咨询

Handling 302 Redirects in Atmosphere WebSocket Client

Absolutely, Atmosphere does provide the ability to handle this 302 redirect scenario—you just need to tweak your client-side configuration to intercept the response before the default behavior takes over. Here's why you're seeing only onerror/onclose triggers right now, and how to fix it:

Why the Default Behavior Isn't Working

By default, Atmosphere automatically follows 302 redirects without triggering your custom listeners. When your server returns a 302 after clearing sessions, the framework tries to redirect the connection silently, which ends up firing the native WebSocket onerror and onclose events instead of your registered Atmosphere callbacks.

Solution 1: Disable Automatic Redirect Handling

The simplest fix is to disable Atmosphere's built-in redirect handling, which lets you catch the 302 response directly in an onResponse callback. Here's how to configure your request:

// Define your Atmosphere request configuration
const atmosphereRequest = {
    url: '/your-websocket-endpoint',
    contentType: 'application/json',
    transport: 'websocket',
    fallbackTransport: 'long-polling',
    redirectHandling: false, // Critical: Turn off auto-redirect
    onResponse: function(response) {
        // Check for the 302 status code
        if (response.status === 302) {
            // Execute your custom business logic
            alert('Your session has expired. Please log in again.');
            window.location.href = '/login'; // Redirect to login page
        }
    },
    onError: function(error) {
        // Fallback handling if the 302 slips through
        console.error('WebSocket connection error:', error);
    },
    onClose: function(event) {
        console.log('WebSocket connection closed');
    }
};

// Subscribe to the endpoint
const socket = atmosphere.subscribe(atmosphereRequest);

Solution 2: Use a Custom Atmosphere Interceptor

For more modular control, you can create a custom interceptor to intercept responses and handle the 302 case. This is useful if you want to reuse this logic across multiple connections:

// Create a custom interceptor
const sessionExpiryInterceptor = {
    onResponse: function(response) {
        if (response.status === 302) {
            // Handle session expiry
            alert('Session expired. Redirecting to login...');
            window.location.href = '/login';
            // Return false to stop further processing of the response
            return false;
        }
        // Return true to let the response proceed to other handlers
        return true;
    }
};

// Configure the request with the interceptor
const atmosphereRequest = {
    url: '/your-websocket-endpoint',
    contentType: 'application/json',
    transport: 'websocket',
    fallbackTransport: 'long-polling',
    interceptors: [sessionExpiryInterceptor] // Add the custom interceptor
};

const socket = atmosphere.subscribe(atmosphereRequest);

Key Notes

  • Fallback Transport: Make sure your configuration includes a fallback transport (like long-polling) if WebSocket isn't supported. The same redirect handling logic will apply to the fallback mechanism.
  • Handshake Phase Check: The 302 is likely returned during the initial WebSocket handshake (HTTP upgrade request). Disabling redirectHandling ensures this response is passed to your callbacks instead of being automatically followed.
  • Cross-Browser Consistency: While most browsers will expose the 302 status in onResponse, testing across target browsers is recommended to ensure consistency.

内容的提问来源于stack exchange,提问作者omalyutin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 10:00:13