Websocket
The WebSocket APIs enable real-time, bidirectional communication between your mini game and a developer server through a persistent connection. Use TTMinis.game.connectSocket to establish a connection, then use the returned SocketTask object to send messages, register event listeners, and manage the connection lifecycle.
Note: Websocket JavaScript APIs are supported starting from SDK version 0.4.0. Lower versions must be adjusted for compatibility.
TTMinis.game.connectSocket(opts)
Creates a WebSocket connection.
Note: Supported from base library version 0.4.0. Lower versions require compatibility handling.
Parameters
Field | Type | Description | Required |
|
| The developer server WSS endpoint URL | Yes |
|
| The HTTP request headers. The Referer header cannot be set. | No |
|
| The sub-protocol array | No |
|
| The timeout duration in milliseconds | No |
|
| The callback function called when the API call succeeds | No |
|
| The callback function called when the API call fails | No |
|
| The callback function called when the API call ends, regardless of success or failure | No |
Returns
A SocketTask object that can be used to monitor and control the WebSocket connection.
Example
TTMinis.game.connectSocket({
url: 'wss://EXAMPLE_WSS_ENDPOINT',
header: {
'content-type': 'application/json'
}
})TTMinis.game.SocketTask.close(opts)
Closes the WebSocket connection.
Parameters
Field | Type | Description | Required |
|
| A numeric status code indicating the reason for closing the connection. Default is 1000 (normal closure). | No |
|
| A human-readable string explaining the reason for closing the connection. Must be no longer than 123 bytes of UTF-8 text (not characters). | No |
|
| The callback function called when the API call succeeds | No |
|
| The callback function called when the API call fails | No |
|
| The callback function called when the API call ends, regardless of success or failure | No |
Example
socketTask.close({
code: 1000,
reason: 'Normal closure'
})TTMinis.game.SocketTask.onOpen(listener)
Registers a listener for the WebSocket connection open event.
Parameters
Field | Type | Description | Required |
|
| The listener function for the WebSocket connection open event | Yes |
Listener callback parameters
Field | Type | Description |
|
| The HTTP response headers from the successful connection |
|
| Debug information from the network request process |
Example
socketTask.onOpen((res) => {
console.log('WebSocket connected', res.header)
})TTMinis.game.SocketTask.onMessage(listener)
Registers a listener for the WebSocket message received event.
Parameters
Field | Type | Description | Required |
|
| The listener function for the WebSocket message received event | Yes |
Listener callback parameters
Field | Type | Description |
|
| The message returned by the server |
Example
socketTask.onMessage((res) => {
console.log('Message received:', res.data)
})TTMinis.game.SocketTask.onClose(listener)
Registers a listener for the WebSocket connection close event.
Parameters
Field | Type | Description | Required |
|
| The listener function for the WebSocket connection close event | Yes |
Listener callback parameters
Field | Type | Description |
|
| A numeric status code indicating the reason for closing the connection |
|
| A human-readable string explaining the reason for closing the connection |
Example
socketTask.onClose((res) => {
console.log('WebSocket closed', res.code, res.reason)
})TTMinis.game.SocketTask.onError(listener)
Registers a listener for the WebSocket error event.
Parameters
Field | Type | Description | Required |
|
| The listener function for the WebSocket error event | Yes |
Listener callback parameters
Field | Type | Description |
|
| The error code |
|
| The error message |
Example
socketTask.onError((res) => {
console.error('WebSocket error', res.errCode, res.errMsg)
})TTMinis.game.SocketTask.send(opts)
Sends data through the WebSocket connection.
Parameters
Field | Type | Description | Required |
|
| The content to send | Yes |
|
| The callback function called when the API call succeeds | No |
|
| The callback function called when the API call fails | No |
|
| The callback function called when the API call ends, regardless of success or failure | No |
Example
socketTask.send({
data: 'Hello, server'
})