From 06b8fc3d7958f78060f6d861e9f4a009973260a5 Mon Sep 17 00:00:00 2001 From: SilverBirchh Date: Fri, 6 Nov 2026 15:48:38 +0000 Subject: [PATCH] fix: allow native DialOptions fields on Wasm DialOptions on Wasm had only Subprotocols, so code that sets HTTPClient or HTTPHeader for native builds did not compile for Wasm, and libraries supporting both had to split each Dial call behind build tags. The Wasm DialOptions now has the native fields and Dial ignores them, since the browser performs the handshake and cannot use them. --- ws_js.go | 15 +++++++++++++++ ws_js_test.go | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/ws_js.go b/ws_js.go index 026b75fc..223a5b23 100644 --- a/ws_js.go +++ b/ws_js.go @@ -278,9 +278,24 @@ func (c *Conn) Subprotocol() string { } // DialOptions represents the options available to pass to Dial. +// +// Only Subprotocols is used. The JavaScript WebSocket API performs the +// handshake and handles control frames, so it cannot send custom headers, use +// another HTTP client, set compression options or report pings and pongs. +// The other fields match the native DialOptions so code that sets them also +// compiles for Wasm. Dial ignores them. type DialOptions struct { + HTTPClient *http.Client + HTTPHeader http.Header + Host string + // Subprotocols lists the subprotocols to negotiate with the server. Subprotocols []string + + CompressionMode CompressionMode + CompressionThreshold int + OnPingReceived func(ctx context.Context, payload []byte) bool + OnPongReceived func(ctx context.Context, payload []byte) } // Dial creates a new WebSocket connection to the given url with the given options. diff --git a/ws_js_test.go b/ws_js_test.go index 1fa242f4..e9ebb9cd 100644 --- a/ws_js_test.go +++ b/ws_js_test.go @@ -37,6 +37,27 @@ func TestWasm(t *testing.T) { assert.Success(t, err) } +func TestWasmDialIgnoresNativeOptions(t *testing.T) { + t.Parallel() + + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + + c, _, err := websocket.Dial(ctx, os.Getenv("WS_ECHO_SERVER_URL"), &websocket.DialOptions{ + HTTPClient: http.DefaultClient, + HTTPHeader: http.Header{"Authorization": []string{"Bearer token"}}, + Host: "example.com", + Subprotocols: []string{"echo"}, + }) + assert.Success(t, err) + defer c.Close(websocket.StatusInternalError, "") + + assert.Equal(t, "subprotocol", "echo", c.Subprotocol()) + + err = c.Close(websocket.StatusNormalClosure, "") + assert.Success(t, err) +} + func TestWasmDialTimeout(t *testing.T) { t.Parallel()