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

Add support for ModifyResponse to alter transmitting messages #38

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
10 changes: 10 additions & 0 deletions websocketproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type WebsocketProxy struct {
// Dialer contains options for connecting to the backend WebSocket server.
// If nil, DefaultDialer is used.
Dialer *websocket.Dialer

// ModifyResponse allows modifying the websocket messages bidirectionally
// User assigned function will be called with the request, response and original message,
// and must return the modified message. If nil, the data is transmitted unaltered.
ModifyResponse func(*http.Request, *http.Response, []byte) []byte
}

// ProxyHandler returns a new http.Handler interface that reverse proxies the
Expand Down Expand Up @@ -191,6 +196,11 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
dst.WriteMessage(websocket.CloseMessage, m)
break
}

if w.ModifyResponse != nil {
msg = w.ModifyResponse(req, resp, msg)
}

err = dst.WriteMessage(msgType, msg)
if err != nil {
errc <- err
Expand Down