# webSocket

WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's H5/iOS/android and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply

WARNING

iOS and h5 provide webSocket default handle. if you use webSocket in android environment. You should provide custom adapter implementation, source:

# API

# WebSocket

create websocket

# WebSocket(url, protocol)

  • @url, string, the URL to which to connect.
  • @protocol, string, the websocket protocol.
const ws = weex.requireModule('webSocket');
ws.WebSocket('ws://echo.websocket.org','');

# send

Transmits data to the server over the WebSocket connection

# send(data)

  • @data, string, a text string to send to the server.
const ws = weex.requireModule('webSocket');
ws.WebSocket('ws://echo.websocket.org','');
ws.send('some message.');

# close

Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, this method does nothing.

# close(code, reason)

  • @code, number, the status code explaining why the connection is being closed.
  • @reason, string, a string explaining why the connection is closing
const ws = weex.requireModule('webSocket');
ws.WebSocket('ws://echo.websocket.org','');
ws.close();

# onopen

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.

# onopen(options)

  • @options, object, an empty object
const ws = weex.requireModule('webSocket')
ws.WebSocket('ws://echo.websocket.org','');
ws.onopen(function(event) {
console.log('onopen', event);
});

# onmessage

An event listener to be called when a message is received from the server

# onmessage(options)

  • @options, object, the server message options.
    • data, string, the listener received message.
const ws = weex.requireModule('webSocket')
ws.WebSocket('ws://echo.websocket.org','');
ws.onmessage(function(event) {
  console.log('onmessage', event);
});

# onclose

An event listener to be called when the WebSocket connection's readyState changes to CLOSED

# onclose(options)

  • @options, object, the CloseEvent is sent to clients using WebSockets when the connection is closed.
    • code, number, returns an unsigned short containing the close code send by the server.
    • reason, string, returns a string indicating the reason the server closed the connection.
    • wasClean, boolen, returns a Boolean that Indicates whether or not the connection was cleanly closed.
const ws = weex.requireModule('webSocket')
ws.WebSocket('ws://echo.websocket.org','');
ws.onclose(function(event) {
  console.log('onclose', event);
});

# onerror

An event listener to be called when an error occurs.

# onerror(options)

  • @options, object, the error event.
    • data, string, the listener received error data.
const ws = weex.requireModule('webSocket')
ws.WebSocket('ws://echo.websocket.org','');
ws.onerror(function(event) {
  console.log('onerror', event);
});

Demos

Last Updated: 8/15/2022
Excellent docUnusable doc