title: SocketTask
sidebar_label: SocketTask
The WebSocket task can be created and returned via the Taro.connectSocket() API.
Reference
| Property |
Type |
Description |
| socketTaskId |
number |
The current connection ID of the websocket. |
| readyState |
number |
The current connection status of the websocket. |
| errMsg |
string |
The result of the call to the websocket interface. |
| CONNECTING |
number |
websocket status value: Connecting. |
| OPEN |
number |
websocket status value: Connected. |
| CLOSING |
number |
websocket status value: Closing. |
| CLOSED |
number |
websocket status value: Closed. |
| ws |
WebSocket |
Browser websocket instances. (Only H5) |
Methods
close
Disables the WebSocket connection
Reference
(option: CloseOption) => void
| Property |
Type |
| option |
CloseOption |
API Support
| API |
WeChat Mini-Program |
Baidu Smart-Program |
Alipay Mini-Program |
H5 |
React Native |
| SocketTask.close |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
onClose
Listens on the event of disabling the WebSocket connection.
Reference
(callback: OnCloseCallback) => void
| Property |
Type |
Description |
| callback |
OnCloseCallback |
The callback function for the event of disabling the WebSocket connection. |
API Support
| API |
WeChat Mini-Program |
Baidu Smart-Program |
Alipay Mini-Program |
H5 |
React Native |
| SocketTask.onClose |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
onError
Listens on the WebSocket error event.
Reference
(callback: OnErrorCallback) => void
| Property |
Type |
Description |
| callback |
OnErrorCallback |
The callback function for the WebSocket error event. |
API Support
| API |
WeChat Mini-Program |
Baidu Smart-Program |
Alipay Mini-Program |
H5 |
React Native |
| SocketTask.onError |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
onMessage
Listens on the event of receiving server messages by WebSocket
Reference
<T = any>(callback: OnMessageCallback<T>) => void
| Property |
Type |
Description |
| callback |
T |
The callback function for the event of receiving server messages by WebSocket. |
API Support
| API |
WeChat Mini-Program |
Baidu Smart-Program |
Alipay Mini-Program |
H5 |
React Native |
| SocketTask.onMessage |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
onOpen
Listens on the event of enabling the WebSocket connection.
Reference
(callback: OnOpenCallback) => void
| Property |
Type |
Description |
| callback |
OnOpenCallback |
The callback function for the event of enabling the WebSocket connection. |
API Support
| API |
WeChat Mini-Program |
Baidu Smart-Program |
Alipay Mini-Program |
H5 |
React Native |
| SocketTask.onOpen |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
send
Sends data over a WebSocket connection
Reference
(option: SendOption) => void
| Property |
Type |
| option |
SendOption |
API Support
| API |
WeChat Mini-Program |
Baidu Smart-Program |
Alipay Mini-Program |
H5 |
React Native |
| SocketTask.send |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
Parameters
CloseOption
| Property |
Type |
Required |
Description |
| code |
number |
No |
A numeric value indicates the status code explaining why the connection has been disabled. |
| reason |
string |
No |
A readable string explaining why the connection has been disabled. |
| complete |
(res: CallbackResult) => void |
No |
The callback function used when the API call completed (always executed whether the call succeeds or fails) |
| fail |
(res: CallbackResult) => void |
No |
The callback function for a failed API call |
| success |
(res: CallbackResult) => void |
No |
The callback function for a successful API call |
OnCloseCallback
The callback function for the event of disabling the WebSocket connection.
(result: OnCloseCallbackResult) => void
| Property |
Type |
| result |
OnCloseCallbackResult |
OnCloseCallbackResult
| Property |
Type |
Description |
| code |
number |
A numeric value indicates the status code explaining why the connection has been disabled. |
| reason |
string |
A readable string explaining why the connection has been disabled. |
OnErrorCallback
The callback function for the WebSocket error event.
(result: OnErrorCallbackResult) => void
| Property |
Type |
| result |
OnErrorCallbackResult |
OnErrorCallbackResult
| Property |
Type |
Description |
| errMsg |
string |
Error message |
OnMessageCallback
The callback function for the event of receiving server messages by WebSocket.
(result: OnMessageCallbackResult<T>) => void
| Property |
Type |
| result |
OnMessageCallbackResult<T> |
OnMessageCallbackResult
| Property |
Type |
Description |
| data |
T |
Messages returned by the server |
OnOpenCallback
The callback function for the event of enabling the WebSocket connection.
(result: OnOpenCallbackResult) => void
| Property |
Type |
| result |
OnOpenCallbackResult |
OnOpenCallbackResult
| Property |
Type |
Description |
| header |
Record<string, any> |
Connected HTTP response header |
SendOption
| Property |
Type |
Required |
Description |
| data |
string | ArrayBuffer |
Yes |
The data to be sent |
| complete |
(res: CallbackResult) => void |
No |
The callback function used when the API call completed (always executed whether the call succeeds or fails) |
| fail |
(res: CallbackResult) => void |
No |
The callback function for a failed API call |
| success |
(res: CallbackResult) => void |
No |
The callback function for a successful API call |
API Support
| API |
WeChat Mini-Program |
Baidu Smart-Program |
Alipay Mini-Program |
H5 |
React Native |
| SocketTask.close |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
| SocketTask.onClose |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
| SocketTask.onError |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
| SocketTask.onMessage |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
| SocketTask.onOpen |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |
| SocketTask.send |
✔️ |
✔️ |
✔️ |
✔️ |
✔️ |