Home > SYS_WebSocket > close
SYS_WebSocket.close() method
Close the WebSocket connection
Signature
typescript
function close(id: string, code?: number, reason?: string, extensionUuid?: string): void;1
Parameters
Parameter | Type | Description |
|---|---|---|
id | string | Custom WebSocket ID |
code | number | (Optional) Numeric status code, corresponding to the codes allowed in WebSocket.CloseEvent |
reason | string | (Optional) A human-readable string explaining why the connection was closed |
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. 先注册一条连接用于演示
eda.sys_WebSocket.register('嘉立创示例_关闭', 'ws://127.0.0.1:49620', () => {}, () => {
console.log('连接已建立');
});
// 2. 等待握手完成
await new Promise(r => setTimeout(r, 1500));
// 3. 以「正常关闭」状态码 1000 关闭连接,并附带原因
eda.sys_WebSocket.close('嘉立创示例_关闭', 1000, '演示完毕');
console.log('已关闭连接(code 1000)');1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11