Skip to content

Module: core/useWebSocket

Classes

Interfaces

Functions

useWebSocket

useWebSocket(url, option?): Promise<WrapperWebSocket>

返回一个基于PromisewebSocket包装器对象。

Example

ts
import { useWebSocket } from '@sdkset/core'

import type { CreateWebSocketOption } from '@sdkset/core'

const option: CreateWebSocketOption = { ... }
const ws = await useWebSocket('ws://124.222.224.186:8800', option)

ws.send('data')
...
ws.close()
import { useWebSocket } from '@sdkset/core'

import type { CreateWebSocketOption } from '@sdkset/core'

const option: CreateWebSocketOption = { ... }
const ws = await useWebSocket('ws://124.222.224.186:8800', option)

ws.send('data')
...
ws.close()

Default

ts
useWebSocket(undefined, {
  params: undefined, // 请求参数
  protocols: undefined, // 协议字符串 or 包含协议字符串的数组
  heartbeat: true, // 心跳配置
  autoReconnect: false, // 重连配置
  interceptor: {}, // 拦截器对象
})

// 心跳(默认开启),布尔值控制开启/关闭,也可自行配置(以下为默认配置):
heartbeat: {
  message: 'ping', // 心跳间隔消息
  interval: 1000 // 心跳间隔毫秒数
}

// 重连(默认关闭),布尔值控制开启/关闭,也可自行配置(以下为默认配置):
heartbeat: {
  retries: 3, // 最大重连次数
  delay: 1000 // 重连间隔毫秒数
}

// 拦截器对象
interceptor: {
  error: () => {} // 连接发生错误时
  message: () => {} // 接收到新消息时
  close: () => {} // 连接关闭时
  fail: () => {} // 重连失败时
}
useWebSocket(undefined, {
  params: undefined, // 请求参数
  protocols: undefined, // 协议字符串 or 包含协议字符串的数组
  heartbeat: true, // 心跳配置
  autoReconnect: false, // 重连配置
  interceptor: {}, // 拦截器对象
})

// 心跳(默认开启),布尔值控制开启/关闭,也可自行配置(以下为默认配置):
heartbeat: {
  message: 'ping', // 心跳间隔消息
  interval: 1000 // 心跳间隔毫秒数
}

// 重连(默认关闭),布尔值控制开启/关闭,也可自行配置(以下为默认配置):
heartbeat: {
  retries: 3, // 最大重连次数
  delay: 1000 // 重连间隔毫秒数
}

// 拦截器对象
interceptor: {
  error: () => {} // 连接发生错误时
  message: () => {} // 接收到新消息时
  close: () => {} // 连接关闭时
  fail: () => {} // 重连失败时
}

Parameters

NameTypeDescription
urlstring请求地址
option?CreateWebSocketOption包装器选项

Returns

Promise<WrapperWebSocket>