WebSocket.close()

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();
WebSocket.close(code);
WebSocket.close(reason);
WebSocket.close(code, reason);

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

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
close
4
12
7
10
12.1
5
≤37
18
7
12.1
4.2
1.0

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/close