has.connectSocket(Object object): SocketTask
Creates a WebSocket connection.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Attribute | Type | Mandatory | Description |
|---|---|---|---|
url | string | Yes | Server wss API URL. |
header | object | No | HTTP request header, which cannot contain Referer. |
protocols | Array<string> | No | Sub-protocol group. |
success | function | No | Callback to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Return value
A SocketTask object, which can be used to operate the socket.
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.
Before calling this API, ensure that you have called has.connectSocket and received the callback.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Name | 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 to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Example
has.sendSocketMessage({
data: 'Hello'
}); has.onSocketOpen(function callback)
Enables listening for WebSocket connection Open events.
Since: 1.0.0
Parameters
Name | Type | Description |
|---|---|---|
callback | function | Callback for the WebSocket connection Open event. |
Return value of callback
Name | Type | Description |
|---|---|---|
header | object | HTTP response header indicating a successful connection. |
Example
has.onSocketOpen(() => {
console.log('onSocketOpen success: ' + JSON.stringify(res));
}); has.onSocketMessage(function callback)
Enables listening for WebSocket connection Message events.
Since: 1.0.0
Parameters
Name | Type | Description |
|---|---|---|
callback | function | Callback for the WebSocket connection Message event. |
Return value of callback
Name | 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. |
Example
has.onSocketMessage(message => {
console.log('socket message:', message);
}); has.onSocketError(function callback)
Enables listening for WebSocket connection Error events.
Since: 1.0.0
Parameters
Name | Type | Description |
|---|---|---|
callback | function | Callback for the WebSocket connection Error event. |
Example
has.onSocketError(error => {
console.error('socket error:', error);
}); has.onSocketClose(function callback)
Enables listening for WebSocket connection Close events.
Since: 1.0.0
Parameters
Name | Type | Description |
|---|---|---|
callback | function | Callback for the WebSocket connection Close event. |
Example
has.onSocketClose(() => {
console.log('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.
Name | 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 to be invoked when the API call is successful. |
fail | function | - | No | Callback to be invoked when the API call fails. |
complete | function | - | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
Example
has.connectSocket({
url: 'ws://www.example.com', // Replace the example URL with the actual URL.
});
has.onSocketOpen(function () {
has.closeSocket();
});
has.onSocketClose(function (res) {
console.log('onSocketClose success');
}); SocketTask.send(Object object)
Sends data over a WebSocket connection.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Name | 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 to be invoked when the API call is successful. |
fail | function | No | Callback to be invoked when the API call fails. |
complete | function | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
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: function () {
console.debug('on socketTask connect success callback receive');
}
});
socketTask.send({
data: 'this is a send message',
success(res) {
console.debug('socket send message success, ' + JSON.stringify(res));
},
fail(res) {
console.debug('socket send message error, ' + JSON.stringify(res));
},
complete() {
console.debug('socket send message complete');
}
}); SocketTask.close(Object object)
Closes a WebSocket connection.
Since: 1.0.0
Parameters
The input parameter is an object containing the following fields.
Name | 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.
|
reason | string | - | No | Reason why the WebSocket connection is closed. |
success | function | - | No | Callback to be invoked when the API call is successful. |
fail | function | - | No | Callback to be invoked when the API call fails. |
complete | function | - | No | Callback to be invoked when the API call is complete, regardless of success or failure. |
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: function () {
console.debug('on socketTask connect success callback receive');
}
});
socketTask.close(); SocketTask.onOpen(function callback)
Enables listening for Open events of the WebSocket connection.
Since: 1.0.0
Parameters
Name | Type | Description |
|---|---|---|
callback | function | Callback for the Open events of the WebSocket connection. |
Return value of callback
Name | Type | Description |
|---|---|---|
header | object | HTTP response header indicating a successful connection. |
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: function () {
console.debug('on socketTask connect success callback receive');
}
});
socketTask.onOpen(function (res) {
console.debug('on socketTask open receive: res=' + JSON.stringify(res));
}); SocketTask.onClose(function callback)
Enables listening for Close events of the WebSocket connection.
Since: 1.0.0
Parameters
Name | Type | Description |
|---|---|---|
callback | function | Callback for the Close events of the WebSocket connection. |
Return value of callback
Name | 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 why the WebSocket connection is closed. |
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: function () {
console.debug('on socketTask connect success callback receive');
}
});
socketTask.onClose(function (res) {
console.debug('on socketTask close receive, res=' + JSON.stringify(res));
}); SocketTask.onError(function callback)
Enables listening for Error events of the WebSocket connection.
Since: 1.0.0
Parameters
Name | Type | Description |
|---|---|---|
callback | function | Callback for the Error events of the WebSocket connection. |
Return value of callback
Name | Type | Description |
|---|---|---|
errMsg | string | Error message. |
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: function () {
console.debug('on socketTask connect success callback receive');
}
});
socketTask.onError(function (res) {
console.debug('on socketTask error receive, res=' + JSON.stringify(res));
}); SocketTask.onMessage(function callback)
Enables listening for Message events of the WebSocket connection.
Since: 1.0.0
Parameters
Name | Type | Description |
|---|---|---|
callback | function | Callback for the Message events of the WebSocket connection. |
Return value of callback
Name | 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. |
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: function () {
console.debug('on socketTask connect success callback receive');
}
});
socketTask.onMessage(function (res) {
console.debug('on socketTask message receive, res=' + JSON.stringify(res));
});