Skip to content

fix: WebSocket 多服务路由隔离 — 每个 Server 使用独立 http.ServeMux - #410

Merged
aceld merged 2 commits into
aceld:masterfrom
YuanErya:dev
Oct 22, 2026
Merged

aceld merged 2 commits into
aceld:masterfrom
YuanErya:dev

Conversation

@YuanErya

Copy link
Copy Markdown
Contributor

问题

启动多个开启了 WebSocket 的 zinx server 时,ListenWebsocketConn() 通过 http.HandleFunc() 将 handler 注册到全局
http.DefaultServeMux,导致:

  • 相同 path:panic http: multiple registrations for /
  • 不同 path:所有 server 都能响应其他 server 注册的路径,相互污染

根因

znet/server.go:ListenWebsocketConn() 使用 http.HandleFunc()(注册到 DefaultServeMux),且 http.ListenAndServe()
传 nil(使用 DefaultServeMux),所有 server 共用同一张全局路由表。

修复

每个 server 创建独立的 http.ServeMux 实例:

  • mux := http.NewServeMux() — 每 server 独立路由表
  • mux.HandleFunc(path, handler) — 注册到私有 mux
  • http.ListenAndServe(addr, mux) — 传入私有 mux,替代 nil

改后多个 server 使用相同 path 启动也不会冲突。

@aceld
aceld merged commit 9654d42 into aceld:master Oct 22, 2026
1 check passed
@aceld

aceld commented Oct 22, 2026

Copy link
Copy Markdown
Owner

@YuanErya 感谢提交PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants