WebSocket APIs function in the same way as standard HTML5 APIs.
API | Description |
|---|---|
Creates a WebSocket object. | |
Closes a WebSocket connection. | |
Sends data. | |
Explicitly specifies the type of the received binary data. The value can be blob or arraybuffer. | |
Obtains the number of remaining bytes to be sent. | |
Obtains the extensions supported by the server. | |
Listens for WebSocket connection closing events. | |
Listens for WebSocket connection opening events. | |
Listens for events that messages are received through the WebSocket connection from the server. | |
Listens for WebSocket error events. | |
Returns the protocol used by the instance. | |
Returns the current status of the instance. | |
Returns the URL of the connection. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
url | string | Yes | URL of the connection. |
protocol | array | No | Supported subprotocol. |
createWebSocket(item, url, request, response, protocol, cert) {
let ws = new WebSocket(url, protocol, cert);
ws.onopen = function () {
item.addEvent("open");
request(ws);
};
ws.onmessage = function (evt) {
item.addEvent("message");
response(evt.data);
};
ws.onerror = function (evt) {
item.addEvent("error");
item.setState(TEST_STATE.ERROR);
};
ws.onclose = function () {
item.addEvent("close");
};
item.setState(TEST_STATE.RUNNING);
return ws;
} Closes a WebSocket connection.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
code | number | No | WebSocket connection closing status. The value is a number. |
reason | string | No | Reason for closing a WebSocket connection. The value is a readable string. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
data | string/arraybuffer | Yes | Content to be sent. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for WebSocket connection closing events. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for WebSocket connection opening events. |
Listens for events that messages are received through the WebSocket connection from the server.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Event triggered when a message from the server is received through the WebSocket connection. |
Parameter | Type | Description |
|---|---|---|
data | string/arraybuffer | Message returned by the server. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
callback | function | Yes | Callback function for WebSocket error events. |
Parameter | Type | Description |
|---|---|---|
errMsg | string | Error message. |