fix: WebSocket 多服务路由隔离 — 每个 Server 使用独立 http.ServeMux#410
Open
YuanErya wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
问题
启动多个开启了 WebSocket 的 zinx server 时,
ListenWebsocketConn()通过http.HandleFunc()将 handler 注册到全局http.DefaultServeMux,导致:http: multiple registrations for /根因
znet/server.go:ListenWebsocketConn()使用http.HandleFunc()(注册到 DefaultServeMux),且http.ListenAndServe()传
nil(使用 DefaultServeMux),所有 server 共用同一张全局路由表。修复
每个 server 创建独立的
http.ServeMux实例:mux := http.NewServeMux()— 每 server 独立路由表mux.HandleFunc(path, handler)— 注册到私有 muxhttp.ListenAndServe(addr, mux)— 传入私有 mux,替代nil改后多个 server 使用相同 path 启动也不会冲突。