WebSocket

open external class WebSocket(url: String, protocols: String = definedExternally) : EventTarget(source)

Provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.

MDN Reference

Constructors

Link copied to clipboard
constructor(url: URL, protocols: String = definedExternally)
constructor(url: String, protocols: ReadonlyArray<String>)
constructor(url: URL, protocols: ReadonlyArray<String>)
constructor(url: String, protocols: String = definedExternally)

Types

Link copied to clipboard
object Companion
Link copied to clipboard
sealed interface ReadyState

Properties

Link copied to clipboard

Returns a string that indicates how binary data from the WebSocket object is exposed to scripts:

Link copied to clipboard

Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using send() but not yet been transmitted to the network.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Returns the extensions selected by the server, if any.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Returns the subprotocol selected by the server, if any. It can be used in conjunction with the array form of the constructor's second argument to perform subprotocol negotiation.

Link copied to clipboard

Returns the state of the WebSocket object's connection. It can have the values described below.

Link copied to clipboard
val url: String

Returns the URL that was used to establish the WebSocket connection.

Functions

Link copied to clipboard
fun close(code: Short = definedExternally, reason: String = definedExternally)

Closes the WebSocket connection, optionally using code as the the WebSocket connection close code and reason as the the WebSocket connection close reason.

Link copied to clipboard
Link copied to clipboard
fun send(data: Blob)

fun send(data: String)

Transmits data using the WebSocket connection. data can be a string, a Blob, an ArrayBuffer, or an ArrayBufferView.