Home > SYS_WebSocket > send
SYS_WebSocket.send() method
Send data to the WebSocket server
Signature
typescript
function send(id: string, data: string | Blob | BufferSource, extensionUuid?: string): void;1
Parameters
Parameter | Type | Description |
|---|---|---|
id | string | Custom WebSocket ID |
data | string | Blob | BufferSource | Data to send |
extensionUuid | string | (Optional) Extension UUID. Generally it does not need to be specified. It only needs to be specified as another extension's UUID when you need to operate on a WebSocket connection established by another extension |
Returns
void
Remarks
Note: This API requires the user to enable the extension external interaction permission, if not enabled, it will always throw Error
Example
javascript
// 1. 注册连接(本例的桥接服务对 ping 消息回 pong,用来演示完整收发回路)
eda.sys_WebSocket.register('嘉立创示例_发送', 'ws://127.0.0.1:49620', (event) => {
console.log('收到服务器回复:', event.data);
}, () => {
console.log('连接已建立');
});
// 2. 等待握手完成
await new Promise(r => setTimeout(r, 1500));
// 3. 发送数据(data 支持字符串、Blob、BufferSource,这里发 JSON 字符串)
eda.sys_WebSocket.send('嘉立创示例_发送', JSON.stringify({ type: 'ping', id: '嘉立创示例_发送' }));
// 4. 等待服务器回复进入 receiveMessageCallFn
await new Promise(r => setTimeout(r, 1500));
// 5. 演示完毕后关闭连接
eda.sys_WebSocket.close('嘉立创示例_发送');
console.log('已关闭连接');1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19