Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement custom mux for Websocket servers #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions core/transport/websocket_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type wsServerTransport struct {
l net.Listener
m map[*Transport]struct{}
done chan struct{}
mux *http.ServeMux
}

func (ws *wsServerTransport) Close() (err error) {
Expand Down Expand Up @@ -80,8 +81,11 @@ func (ws *wsServerTransport) Listen(ctx context.Context, notifier chan<- bool) (
}
}()

mux := http.NewServeMux()
mux.HandleFunc(ws.path, func(w http.ResponseWriter, r *http.Request) {
if ws.mux == nil {
ws.mux = http.NewServeMux()
}

ws.mux.HandleFunc(ws.path, func(w http.ResponseWriter, r *http.Request) {
// upgrade websocket
c, err := ws.upgrader.Upgrade(w, r, nil)
if err != nil {
Expand All @@ -103,7 +107,7 @@ func (ws *wsServerTransport) Listen(ctx context.Context, notifier chan<- bool) (
}
})

err = http.Serve(ws.l, mux)
err = http.Serve(ws.l, ws.mux)
if err == io.EOF || isClosedErr(err) {
err = nil
} else {
Expand Down Expand Up @@ -138,8 +142,12 @@ func (ws *wsServerTransport) putTransport(tp *Transport) bool {
}
}

func NewWebsocketServerTransportWithMux(f ListenerFactory, path string, upgrader *websocket.Upgrader, mux *http.ServeMux) ServerTransport {
return NewWebsocketServerTransport(f, path, upgrader, mux)
}

// NewWebsocketServerTransport creates a new server-side transport.
func NewWebsocketServerTransport(f ListenerFactory, path string, upgrader *websocket.Upgrader) ServerTransport {
func NewWebsocketServerTransport(f ListenerFactory, path string, upgrader *websocket.Upgrader, mux *http.ServeMux) ServerTransport {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to keep the original NewWebsocketServerTransport method signature, I think.

For example, extract a private method:

func innerNewWebsocketServerTransport(f ListenerFactory, path string, upgrader *websocket.Upgrader, mux *http.ServeMux) ServerTransport {
      // original codes of NewWebsocketServerTransport
}

func NewWebsocketServerTransportWithMux(f ListenerFactory, path string, upgrader *websocket.Upgrader, mux *http.ServeMux) ServerTransport {
    return innerNewWebsocketServerTransport(f,path,upgrader,mux)
}

func NewWebsocketServerTransport(f ListenerFactory, path string, upgrader *websocket.Upgrader) ServerTransport {
    return innerNewWebsocketServerTransport(f,path,upgrader,nil)
}

if path == "" {
path = defaultWebsocketPath
}
Expand All @@ -158,6 +166,7 @@ func NewWebsocketServerTransport(f ListenerFactory, path string, upgrader *webso
f: f,
m: make(map[*Transport]struct{}),
done: make(chan struct{}),
mux: mux,
}
}

Expand All @@ -174,7 +183,7 @@ func NewWebsocketServerTransportWithAddr(addr string, path string, upgrader *web
}
return tls.NewListener(l, config), nil
}
return NewWebsocketServerTransport(f, path, upgrader)
return NewWebsocketServerTransport(f, path, upgrader, nil)
}

// NewWebsocketClientTransport creates a new client-side transport.
Expand Down
Loading