Web/API/WebSocket/close

From Get docs

The WebSocket.close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.

Syntax

WebSocket.close();

Parameters

code Optional
A numeric value indicating the status code explaining why the connection is being closed. If this parameter is not specified, a default value of 1005 is assumed. See the list of status codes of CloseEvent for permitted values.
reason Optional
A human-readable string explaining why the connection is closing. This string must be no longer than 123 bytes of UTF-8 text (not characters).

Exceptions thrown

INVALID_ACCESS_ERR
An invalid code was specified.
SYNTAX_ERR
The reason string is too long or contains unpaired surrogates.

Note: In Gecko, this method didn't support any parameters prior to Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5).


Specifications

Specification Status Comment
[https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-close HTML Living Standard

The definition of 'WebSocket.close()' in that specification.]

Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
close Chrome

Full support 4

Edge

Full support 12

Firefox Full support 8


Full support 8


No support 4 — 8

Notes'

Notes' Parameters not supported, see bug 674716.

IE

Full support 10

Opera

Full support 12.1

Safari

Full support 5

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android Full support 8


Full support 8


No support 4 — 8

Notes'

Notes' Parameters not supported, see bug 674716.

Opera Android

Full support 12.1

Safari iOS

Full support 4.2

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
See implementation notes.'
See implementation notes.