JavaScripture
Contribute via GitHub Feedback

WebSocket : EventTarget

WebSockets are a persistent connection to a server that allows sending and receiving data. See also EventSource.

Constructors

new WebSocket(url : String) : WebSocket

Constructs a new WebSocket connection to url.

Example:

Run

Results:

 

new WebSocket(url : String, protocol : String) : WebSocket
new WebSocket(url : String, protocols : Iterable<String>) : WebSocket

Instance Properties

binaryType : String

Set to either 'arraybuffer' or 'blob' to choose if the MessageEvent.data property will be an ArrayBuffer or a Blob for binary data received by the WebSocket.

Example:

Run

Results:

 

bufferedAmount : Number  

extensions : String  

protocol : String  

readyState : Number  

One of WebSocket.CONNECTING, WebSocket.OPEN, WebSocket.CLOSING, or WebSocket.CLOSED.

Example:

Run

Results:

 

url : String  

The url passed to the WebSocket constructor.

Example:

Run

Results:

 

Instance Methods

close([code : Number, [reason : String]]) : undefined

Closes the WebSocket connection.

send 4 variants
send(data : ArrayBuffer) : undefined

Sends data to the server.

send(data : ArrayBufferView) : undefined

Sends data to the server.

send(data : Blob) : undefined

Sends data to the server.

send(data : String) : undefined

Sends data to the server.

Instance Events

onclose / 'close'  
listener(event : CloseEvent) : undefined

onerror / 'error'  
listener(event : ErrorEvent) : undefined

onmessage / 'message'  
listener(event : MessageEvent) : undefined

onopen / 'open'  
listener(event : Event) : undefined

WebSocket Properties

CLOSED : Number    

The value of readyState after onclose fires.

CLOSING : Number    

The value of readyState after calling close() and before onclose fires.

CONNECTING : Number    

The value of readyState after constructing the WebSocket and before onopen fires.

OPEN : Number    

The value of readyState after onopen fires.