has.connectSocket(Object object): SocketTask
Creates a WebSocket connection.
The API address of the developer server must use the WSS protocol and the server domain name must be configured in AppGallery Connect.
A global instance is generated if there is no global instance. You can use has.sendSocketMessage to send messages.
A maximum of five global instances can exist in an atomic service.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| url | string | Yes | WSS API URL of the server. |
| header | object | No | HTTP request header, which cannot contain Referer. |
| protocols | Array<string> | No | Protocols. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Returns:
A SocketTask object, which can be used for socket operations.
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: current socket is close. |
| 102 | Parameter error. Possible causes: check type is invalid. |
| 201 | Permission denied. |
| 401 | Parameter error. Possible causes: 1. Missing mandatory parameters. 2. Incorrect parameter type. 3. Parameter verification failed. |
| 40000501 | connectSocket error. For details, see WebSocket Error Codes. |
Example:
has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
header: {
'content-type': 'application/json'
},
protocols: ['protocol1']
}); has.sendSocketMessage(Object object)
Sends data over a successfully established WebSocket connection.
You need to call has.connectSocket first and send data only after has.onSocketOpen receives the callback. Otherwise, the data fails to be sent.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| data | string | ArrayBuffer | Yes | Content to be sent. In version 1.0.3 or later, only data of the ArrayBuffer type can be sent. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: current socket is close. |
| 102 | Parameter error. Possible causes: check type is invalid. |
| 401 | Parameter error. Possible causes: 1. Missing mandatory parameters. 2. Incorrect parameter type. 3. Parameter verification failed. |
| 40000901 | sendSocketMessage error. For details, see WebSocket Error Codes. |
Example:
has.sendSocketMessage({
data: 'Hello'
}); has.onSocketOpen(function callback)
Enables listening for WebSocket connection opening events.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for WebSocket connection opening events. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| header | object | HTTP response header indicating a successful connection. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: current socket is close. |
| 102 | Parameter error. Possible causes: listener should be a function. |
| 40000701 | onSocketOpen error. For details, see WebSocket Error Codes. |
Example:
has.onSocketOpen((res) => {
console.info('onSocketOpen success', res);
}); has.onSocketMessage(function callback)
Enables listening for WebSocket connection message events.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for WebSocket connection message events. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| data | string | ArrayBuffer | Message returned by the server. In version 1.0.3 or later, only data of the ArrayBuffer type can be received. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: current socket is close. |
| 102 | Parameter error. Possible causes: listener should be a function. |
| 40001001 | onSocketMessage error. For details, see WebSocket Error Codes. |
Example:
has.onSocketMessage(res => {
console.info('socket message', res);
}); has.onSocketError(function callback)
Enables listening for WebSocket connection error events.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for WebSocket error events. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: current socket is close. |
| 102 | Parameter error. Possible causes: listener should be a function. |
| 40001101 | onSocketError error. For details, see WebSocket Error Codes. |
Example:
has.onSocketError(err => {
console.error('socket error', err);
}); has.onSocketClose(function callback)
Enables listening for WebSocket connection closing events.
Since: 1.0.0
Parameters
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| callback | function | Yes | Callback for WebSocket connection closing events. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: current socket is close. |
| 102 | Parameter error. Possible causes: listener should be a function. |
| 40000801 | onSocketClose error. For details, see WebSocket Error Codes. |
Example:
has.onSocketClose(() => {
console.info('onSocketClose success');
}); has.closeSocket(Object object)
Closes a WebSocket connection.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| code | number | 1000 (normal disconnection) | No | Status code of the closed connection. |
| reason | string | - | No | Reason why the connection is closed. |
| success | function | - | No | Callback triggered when the API call is successful. |
| fail | function | - | No | Callback triggered when the API call fails. |
| complete | function | - | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 101 | Common error. Possible causes: current socket is close. |
| 102 | Parameter error. Possible causes: check type is invalid. |
| 40000601 | closeSocket error. For details, see WebSocket Error Codes. |
Example:
has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
});
has.onSocketOpen(() => {
has.closeSocket();
});
has.onSocketClose((res) => {
console.info('onSocketClose success');
}); SocketTask.send(Object object)
Sends data through the WebSocket connection.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Mandatory | Description |
|---|---|---|---|
| data | string | ArrayBuffer | Yes | Content to be sent. In version 1.0.3 or later, only data of the ArrayBuffer type can be sent. |
| success | function | No | Callback triggered when the API call is successful. |
| fail | function | No | Callback triggered when the API call fails. |
| complete | function | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: check type is invalid. |
Example:
let socketTask = has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
header: {
'content-type': 'application/json'
},
protocols: ['protocol1'],
method: 'GET',
success: () => {
console.info('on socketTask connect success callback receive');
}
});
socketTask.send({
data: 'this is a send message',
success: (res) => {
console.info('socket send message success', res);
},
fail: (err) => {
console.error('socket send message error', err);
},
complete: (res) => {
console.info('socket send message complete', res);
}
}); SocketTask.close(Object object)
Closes a WebSocket connection.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
| Parameter | Type | Default Value | Mandatory | Description |
|---|---|---|---|---|
| code | number | 1000 | No | Status code of the closed connection, which is used to identify the reason why the connection is closed. 1000: The connection is closed normally. 1006: An error occurs in the WebSocket connection and the connection is closed. |
| reason | string | - | No | Reason why the WebSocket connection is closed. |
| success | function | - | No | Callback triggered when the API call is successful. |
| fail | function | - | No | Callback triggered when the API call fails. |
| complete | function | - | No | Callback triggered when the API call is complete, regardless of success or failure. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: check type is invalid. |
Example:
let socketTask = has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
header: {
'content-type': 'application/json'
},
protocols: ['protocol1'],
method: 'GET',
success: () => {
console.info('on socketTask connect success callback receive');
}
});
socketTask.close(); SocketTask.onOpen(function callback)
Listens for WebSocket connection opening events.
Since: 1.0.0
Parameters
| Parameter | Type | Description |
|---|---|---|
| callback | function | Callback for WebSocket connection opening events. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| header | object | HTTP response header indicating a successful connection. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
let socketTask = has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
header: {
'content-type': 'application/json'
},
protocols: ['protocol1'],
method: 'GET',
success: () => {
console.info('on socketTask connect success callback receive');
}
});
socketTask.onOpen((res) => {
console.info('on socketTask open receive', res);
}); SocketTask.onClose(function callback)
Listens for WebSocket connection closing events.
Since: 1.0.0
Parameters
| Parameter | Type | Description |
|---|---|---|
| callback | function | Callback for WebSocket connection closing events. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| code | number | Status code of the closed connection, which is used to identify the reason why the connection is closed. |
| reason | string | Reason for closing a WebSocket connection. The value is a readable string. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
let socketTask = has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
header: {
'content-type': 'application/json'
},
protocols: ['protocol1'],
method: 'GET',
success: () => {
console.info('on socketTask connect success callback receive');
}
});
socketTask.onClose((res) => {
console.info('on socketTask close receive', res);
}); SocketTask.onError(function callback)
Listens for WebSocket error events.
Since: 1.0.0
Parameters
| Parameter | Type | Description |
|---|---|---|
| callback | function | Callback for WebSocket error events. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| errMsg | string | Error message. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
let socketTask = has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
header: {
'content-type': 'application/json'
},
protocols: ['protocol1'],
method: 'GET',
success: () => {
console.info('on socketTask connect success callback receive');
}
});
socketTask.onError((res) => {
console.error('on socketTask error receive', res);
}); SocketTask.onMessage(function callback)
Listens for events that messages are received through the WebSocket connection from the server.
Since: 1.0.0
Parameters
| Parameter | Type | Description |
|---|---|---|
| callback | function | Callback for WebSocket connection message events. |
Return value of callback
| Parameter | Type | Description |
|---|---|---|
| data | string | ArrayBuffer | Message returned by the server. In version 1.0.3 or later, only data of the ArrayBuffer type can be received. |
Error code information
| Error Code | Error Message |
|---|---|
| 102 | Parameter error. Possible causes: listener should be a function. |
Example:
let socketTask = has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
header: {
'content-type': 'application/json'
},
protocols: ['protocol1'],
method: 'GET',
success: () => {
console.info('on socketTask connect success callback receive');
}
});
socketTask.onMessage((res) => {
console.debug('on socketTask message receive', res);
});