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

使用Socket.IO v4出现Invalid Namespace错误,求排查解决方法

解决Socket.IO v4的"Invalid Namespace"错误

我使用Socket.IO(v4)作为WebSocket库实现通信,配置如下,但出现了"Invalid Namespace"错误,已尝试Nginx粘性会话和Kubernetes Service亲和性配置,网络路径为:用户→Nginx→Kubernetes中的Traefik→Service→Deployment-Pod,求修复方案。

服务端代码

// websocket
const websocketServer = new Server(httpServer, {
  cors: {
    origin: ["https://tex.example.top", "*"],
    credentials: true,
    allowedHeaders: ["*"],
    methods: ["GET", "HEAD", "OPTIONS","POST"],
  },
  path: "/socket.io/"
});

客户端配置

let options: Partial<ManagerOptions & SocketOptions> = {
    withCredentials: true,
    path: "/socket.io/"
  };

错误信息

44/d2f240bc84954f59af1bd1be22113373,{message: "Invalid namespace"}
message
: 
"Invalid namespace"

对应HTTP请求

curl 'https://socket.example.top/socket.io/?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOjEwMywiZGV2aWNlSWQiOiJmZjY1YWJjZWNkYjQ1YmNjZThmYTJkZThiZDEwZWVjYiIsImFwcElkIjoibjI5UGEyOVdTMSIsImx0IjoxLCJldCI6MCwicGlkIjoxMywiZXhwIjoxNzM5MTE3ODMzfQ.1IVMKbc9y8NiTkN_fAetq8L1Pob91fmvbOxG4WqtihQ&from=web_tex_editor&EIO=4&transport=polling&t=qbrfm94f&sid=yyA8lfOrThCzvjcFAAAE' \
  -H 'Accept: */*' \
  -H 'Accept-Language: en,zh-CN;q=0.9,zh;q=0.8,zh-TW;q=0.7,fr;q=0.6' \
  -H 'Connection: keep-alive' \
  -H 'DNT: 1' \
  -H 'Origin: https://tex.poemhub.top' \
  -H 'Referer: https://tex.poemhub.top/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: same-site' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36' \
  -H 'sec-ch-ua: "Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"'

已尝试的配置

Nginx粘性会话配置

server {
    listen       443 ssl;
    server_name  socket.example.top;

    ssl_certificate /etc/letsencrypt/live/example/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example/privkey.pem;

    if ($allowed = 0) {
        return 403;
    }

    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://nodes;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
    }

    location /socket.io/ {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://nodes;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
    }
}

upstream nodes {
    # enable sticky session with either "hash" (uses the complete IP address)
    hash $remote_addr consistent;

    server 127.0.0.1:8000;
}

Kubernetes Service注解配置

apiVersion: v1
kind: Service
metadata:
  name: tex-socketio-service
  namespace: reddwarf-pro
  uid: 2a19e6ad-2516-4cff-b854-de3e0d760f7f
  resourceVersion: '34727656'
  creationTimestamp: '2025-02-03T08:54:25Z'
  labels:
    k8slens-edit-resource-version: v1
  annotations:
    traefik.ingress.kubernetes.io/affinity: "true"
    traefik.ingress.kubernetes.io/load-balancer-method: drr

修复建议

  • 修正CORS Origin配置:服务端CORS的origin配置中,*与credentials: true冲突,需明确添加客户端请求的Origin(https://tex.poemhub.top)到origin数组,替换掉*。
  • 检查代理路径转发:Nginx的location /socket.io/下的proxy_pass需添加末尾斜杠,改为proxy_pass http://nodes/;,避免路径拼接错误导致命名空间识别异常。
  • 验证命名空间匹配:确认客户端连接时未指定服务端未定义的命名空间。若客户端使用默认命名空间/,需确保服务端正确初始化默认命名空间(无需额外创建,Socket.IO默认提供)。
  • 确认会话粘性生效:在服务端打印连接的sid,验证同一客户端的请求是否路由到同一Pod。同时补充Traefik会话Cookie配置,添加traefik.ingress.kubernetes.io/session-cookie-name: "socketio_session"注解,确保亲和性规则生效。
  • 核对版本兼容性:确认客户端与服务端均为Socket.IO v4版本,版本不匹配可能导致协议解析错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 14:05:56