diff --git a/ws_js.go b/ws_js.go index 95f13e1a..0eb2ddc5 100644 --- a/ws_js.go +++ b/ws_js.go @@ -284,9 +284,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 a1d846fa..4d157bbd 100644 --- a/ws_js_test.go +++ b/ws_js_test.go @@ -38,6 +38,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 TestWasmCloseReasonTruncated(t *testing.T) { t.Parallel()