close

abstract suspend fun close(code: Int = WebSocketCloseCodes.NORMAL_CLOSURE, reason: String? = null)(source)

Sends a web socket close frame with the given code and reason, and closes the connection.

The code can be any of the WebSocketCloseCodes defined by the specification, except NO_STATUS_CODE and NO_CLOSE_FRAME which are reserved for representing the absence of close code or close frame and should not be sent in a frame (as defined by the specification in section 7.4.1 of RFC-6455).

The reason must not be longer than 123 bytes (not characters!) when encoded in UTF-8, due to the limit on control frames defined by the web socket protocol specification RFC-6455. You can use String.truncateToCloseFrameReasonLength if you don't control the length of the reason and yet still want to avoid exceptions.