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
6 changes: 5 additions & 1 deletion ws_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func (c *Conn) init() {
})

c.releaseOnError = c.ws.OnError(func(v js.Value) {
// A close event always follows. Closing here would wait for it
// inside this callback and deadlock.
c.setCloseErr(errors.New(v.Get("message").String()))
c.closeWithInternal()
})

c.releaseOnMessage = c.ws.OnMessage(func(e wsjs.MessageEvent) {
Expand Down Expand Up @@ -259,6 +260,9 @@ func (c *Conn) exportedClose(code StatusCode, reason string) error {
})

c.setCloseErr(ce)
if code != StatusNormalClosure && (code < 3000 || code > 4999) {
code = StatusNormalClosure // Browsers throw on any other code.
}
err := c.ws.Close(int(code), reason)
if err != nil {
return err
Expand Down
28 changes: 28 additions & 0 deletions ws_js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,31 @@ func TestWasmDialTimeout(t *testing.T) {
t.Fatal("wasm context dial timeout is not working", time.Since(beforeDial))
}
}

func TestWasmCloseStatus(t *testing.T) {
t.Parallel()

// The library closes with these itself, but browsers only accept
// 1000 and 3000-4999.
for _, code := range []websocket.StatusCode{
websocket.StatusGoingAway,
websocket.StatusPolicyViolation,
websocket.StatusMessageTooBig,
websocket.StatusInternalError,
} {
t.Run(code.String(), func(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{
Subprotocols: []string{"echo"},
})
assert.Success(t, err)

err = c.Close(code, "")
assert.Success(t, err)
})
}
}
Loading