Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

WebSocket object

Creates a bidirectional connection to a remote host. The connection is made and opens when a new WebSocket is created.

Syntax


 var mySocket = new WebSocket(URL, [protocols]);

DOM Information

Inheritance Hierarchy

The WebSocket does not inherit from any class or interface.

Members

The WebSocket object has these types of members:

Methods

The WebSocket object has these methods.

MethodDescription
close

Closes a WebSocket connection.

send

Sends data to the server using a WebSocket connection.

 

Remarks

The WebSocket protocol specification defines two URI schemes ws:// and wss:// for unencrypted and encrypted connections, respectively. For example, you could create a new WebSocket connection with the string "ws://example.com:1234/resource". The URL specifies the host to connect to, the port, the resource you want to use.

The protocol parameter enables you to specify a subprotocol, such as chat or rpc, that you want to use with the WebSocket. Typically the app and the server are matched to use a specific protocol.

Note  Secure connections (wss://) are recommended in most cases because they are more reliable. When proxies or network intermediaries are present, there is a higher probability that secure connections will be successful, as intermediaries are less inclined to attempt to transform secure traffic.

WebSocket connections are bidirectional; communication can flow in either direction without specific requests and responses. Data can be text or binary.

To open a use a WebSocket connection:

  • Create a WebSocket connection with a specific URL and one or more optional sub protocols, such as "chat". You can use the url property to see how the URL was parsed.
  • Set up event handlers for the connection. Events include onopen, onmessage, onerror, and onclose. Use addEventListener to listen for events and removeEventListener when you no longer want to listen.
  • Send data to the host using the send method.
  • Close the connection when you are finished with the close method.

You can check the status of the Websocket connection by:

  • Check the type of data that is received using the binaryType, protocol, and extensions properties.
  • Determine the rate at which your data is moving with the bufferedAmount property.[[ see text to rewrite]]
  • Determine the state of the connection with the readyState property. This state will change as the communication proceeds.

See also

WebSockets in Windows
WebSockets RFC 6455
The WebSocket API

 

 

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.