Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions ws_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 21 additions & 0 deletions ws_js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading