public interface WebSocket extends EventTarget
This is an experimental feature
Because this feature is still in development in some browsers, check the compatibility table for the proper prefixes to use in various browsers.
The WebSocket
object provides the API for creating and managing a WebSocket connection to a server, as well as for sending and receiving data on the connection.
Modifier and Type | Field and Description |
---|---|
static int |
CLOSED
The connection is closed or couldn't be opened.
|
static int |
CLOSING
The connection is in the process of closing.
|
static int |
CONNECTING
The connection is not yet open.
|
static int |
OPEN
The connection is open and ready to communicate.
|
Modifier and Type | Method and Description |
---|---|
EventRemover |
addEventListener(String type,
EventListener listener)
Register an event handler of a specific event type on the
EventTarget . |
EventRemover |
addEventListener(String type,
EventListener listener,
boolean useCapture)
Register an event handler of a specific event type on the
EventTarget . |
void |
close()
Closes the WebSocket connection or connection attempt, if any.
|
void |
close(int code)
Closes the WebSocket connection or connection attempt, if any.
|
void |
close(int code,
String reason)
Closes the WebSocket connection or connection attempt, if any.
|
boolean |
dispatchEvent(Event evt)
Dispatch an event to this
EventTarget . |
String |
getBinaryType()
A string indicating the type of binary data being transmitted by the connection.
|
int |
getBufferedAmount()
The number of bytes of data that have been queued using calls to but not yet transmitted to the network.
|
String |
getExtensions()
The extensions selected by the server.
|
EventListener |
getOnclose()
An event listener to be called when the WebSocket connection's
readyState changes to CLOSED . |
EventListener |
getOnerror()
An event listener to be called when an error occurs.
|
EventListener |
getOnmessage()
An event listener to be called when a message is received from the server.
|
EventListener |
getOnopen()
An event listener to be called when the WebSocket connection's
readyState changes to OPEN ; this indicates that the connection is ready to send and receive data. |
String |
getProtocol()
A string indicating the name of the sub-protocol the server selected; this will be one of the strings specified in the
protocols parameter when creating the WebSocket object. |
int |
getReadyState()
The current state of the connection; this is one of the Ready state constants.
|
String |
getUrl()
The URL as resolved by the constructor.
|
String |
getURL() |
void |
removeEventListener(String type,
EventListener listener)
Removes an event listener from the
EventTarget . |
void |
removeEventListener(String type,
EventListener listener,
boolean useCapture)
Removes an event listener from the
EventTarget . |
boolean |
send(String data)
Transmits data to the server over the WebSocket connection.
|
void |
setBinaryType(String arg) |
void |
setOnclose(EventListener arg) |
void |
setOnerror(EventListener arg) |
void |
setOnmessage(EventListener arg) |
void |
setOnopen(EventListener arg) |
static final int CLOSED
static final int CLOSING
static final int CONNECTING
static final int OPEN
String getURL()
String getBinaryType()
Blob
objects are being used or "arraybuffer" if ArrayBuffer
objects are being used.void setBinaryType(String arg)
int getBufferedAmount()
String getExtensions()
EventListener getOnclose()
readyState
changes to CLOSED
. The listener receives a CloseEvent
named "close".void setOnclose(EventListener arg)
EventListener getOnerror()
void setOnerror(EventListener arg)
EventListener getOnmessage()
MessageEvent
named "message".void setOnmessage(EventListener arg)
EventListener getOnopen()
readyState
changes to OPEN
; this indicates that the connection is ready to send and receive data. The event is a simple one with the name "open".void setOnopen(EventListener arg)
String getProtocol()
protocols
parameter when creating the WebSocket object.int getReadyState()
String getUrl()
EventRemover addEventListener(String type, EventListener listener)
EventTarget
EventTarget
.addEventListener
in interface EventTarget
EventRemover addEventListener(String type, EventListener listener, boolean useCapture)
EventTarget
EventTarget
.addEventListener
in interface EventTarget
void close()
Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED
, this method does nothing.
code
Optional
CloseEvent
page for permitted values.reason
Optional
INVALID_ACCESS_ERR
code
was specified.SYNTAX_ERR
reason
string is too long or contains unpaired surrogates.void close(int code)
Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED
, this method does nothing.
code
Optional
CloseEvent
page for permitted values.reason
Optional
INVALID_ACCESS_ERR
code
was specified.SYNTAX_ERR
reason
string is too long or contains unpaired surrogates.void close(int code, String reason)
Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED
, this method does nothing.
code
Optional
CloseEvent
page for permitted values.reason
Optional
INVALID_ACCESS_ERR
code
was specified.SYNTAX_ERR
reason
string is too long or contains unpaired surrogates.boolean dispatchEvent(Event evt)
EventTarget
EventTarget
.dispatchEvent
in interface EventTarget
void removeEventListener(String type, EventListener listener)
EventTarget
EventTarget
.removeEventListener
in interface EventTarget
void removeEventListener(String type, EventListener listener, boolean useCapture)
EventTarget
EventTarget
.removeEventListener
in interface EventTarget
boolean send(String data)
Transmits data to the server over the WebSocket connection.
data
INVALID_STATE_ERR
OPEN
.SYNTAX_ERR
Gecko's implementation of the send()
method differs somewhat from the specification in Gecko 6.0; Gecko returns a boolean
indicating whether or not the connection is still open (and, by extension, that the data was successfully queued or transmitted); this is corrected in Gecko 8.0 (Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5)
. In addition, at this time, Gecko does not support ArrayBuffer
or Blob
data types.
Copyright © 2018. All rights reserved.