WebSocket.close

Closes the connection, sending a graceful teardown message to the other side. If you provide no arguments, it sends code 1000, normal closure. If you provide a code, you should also provide a short reason string.

  1. void close()
  2. void close(int code, string reason)
    class WebSocket
    void
    close
    (
    int code
    ,
    string reason
    )

Parameters

code int

reason code.

0-999 are invalid. 1000-2999 are defined by the RFC. https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1 1000 - normal finish 1001 - endpoint going away 1002 - protocol error 1003 - unacceptable data received (e.g. binary message when you can't handle it) 1004 - reserved 1005 - missing status code (should not be set except by implementations) 1006 - abnormal connection closure (should only be set by implementations) 1007 - inconsistent data received (i.e. utf-8 decode error in text message) 1008 - policy violation 1009 - received message too big 1010 - client aborting due to required extension being unsupported by the server 1011 - server had unexpected failure 1015 - reserved for TLS handshake failure 3000-3999 are to be registered with IANA. 4000-4999 are private-use custom codes depending on the application. These are what you'd most commonly set here.

reason string

<= 123 bytes of human-readable reason text, used for logs and debugging

See Also

Meta

History

The default code was changed to 1000 on January 9, 2023. Previously it was 0, but also ignored anyway.

On May 11, 2024, the optional arguments were changed to overloads since if you provide a code, you should also provide a reason.