uni.connectSocket(options)
创建一个 WebSocket 连接。
参数
名称 | 类型 | 必填 | 默认值 | 描述 |
options | ConnectSocketOptions | 是 | - | |
名称 | 类型 | 必备 | 默认值 | 描述 | url | string | 是 | - | 开发者服务器接口地址 | header | UTSJSONObject | null | 否 | null | 如果属性名存在,且类型为UTSJSONObject返回对应的结果,不存在返回null | protocols | Array<string> | null | 否 | null | 子协议数组 | success | (result: ConnectSocketSuccess) => void | null | 否 | null | 接口调用成功的回调函数 | fail | (result: ConnectSocketFail) => void | null | 否 | null | 接口调用失败的回调函数 | complete | (result: any) => void | null | 否 | null | 接口调用结束的回调函数(调用成功、失败都会执行) |
|
ConnectSocketSuccess 的属性值
名称 | 类型 | 必备 | 默认值 | 描述 |
errMsg | string | 是 | - | - |
ConnectSocketFail 的属性值
名称 | 类型 | 必备 | 默认值 | 描述 |
errCode | number | 是 | - | 错误码 - 600009 URL格式不合法 |
errSubject | string | 是 | - | 统一错误主题(模块)名称 |
data | any | null | 否 | - | 错误信息中包含的数据 |
cause | Error | null | 否 | - | 源错误信息,可以包含多个错误,详见SourceError |
errMsg | string | 是 | - | - |
ConnectSocketOptions 兼容性
| Android | iOS | web |
url | 3.9 | 4.11 | - |
header | 3.9 | 4.11 | - |
protocols | 3.9 | 4.11 | - |
返回值
SocketTask 的方法
send(options)
通过 WebSocket 连接发送数据
参数
SendSocketMessageFail 的属性值
名称 | 类型 | 必备 | 默认值 | 描述 |
errCode | 10001 | 10002 | 602001 | 是 | - | 错误码 - 10001 发送数据超限,发送队列不能超过16M大小。 - 10002 websocket未连接 - 602001 websocket系统错误 |
errSubject | string | 是 | - | 统一错误主题(模块)名称 |
data | any | null | 否 | - | 错误信息中包含的数据 |
cause | Error | null | 否 | - | 源错误信息,可以包含多个错误,详见SourceError |
errMsg | string | 是 | - | - |
SendSocketMessageOptions 兼容性
| Android | iOS | web |
data | 3.9 | 4.11 | - |
send 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
close(options)
关闭 WebSocket 连接
参数
名称 | 类型 | 必填 | 默认值 | 描述 |
options | CloseSocketOptions | 是 | - | |
名称 | 类型 | 必备 | 默认值 | 描述 | code | number | null | 否 | 1000 | 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。如果这个参数没有被指定,默认的取值是1000 (表示正常连接关闭) | reason | string | null | 否 | "" | 一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于123字节的UTF-8 文本(不是字符) | success | (result: GeneralCallbackResult) => void | null | 否 | null | 接口调用成功的回调函数 | fail | (result: GeneralCallbackResult) => void | null | 否 | null | 接口调用失败的回调函数 | complete | (result: GeneralCallbackResult) => void | null | 否 | null | 接口调用结束的回调函数(调用成功、失败都会执行) |
|
CloseSocketOptions 兼容性
| Android | iOS | web |
code | 3.9 | 4.11 | - |
reason | 3.9 | 4.11 | - |
close 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
onOpen(callback)
监听 WebSocket 连接打开事件
参数
OnSocketOpenCallbackResult 兼容性
| Android | iOS | web |
header | 3.9 | 4.11 | - |
onOpen 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
onClose(callback)
监听 WebSocket 连接关闭事件
参数
名称 | 类型 | 必填 | 默认值 | 描述 |
callback | (result: any) => void | 是 | - | |
onClose 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
onError(callback)
监听 WebSocket 错误
参数
onError 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
onMessage(callback)
监听 WebSocket 接受到服务器的消息事件
参数
OnSocketMessageCallbackResult 的属性值
名称 | 类型 | 必备 | 默认值 | 描述 |
data | any | 是 | - | 服务器返回的消息 |
OnSocketMessageCallbackResult 兼容性
| Android | iOS | web |
data | 3.9 | 4.11 | - |
onMessage 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
connectSocket 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
相关 Bug
uni.onSocketOpen(options)
监听WebSocket连接打开事件。
参数
onSocketOpen 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
相关 Bug
uni.onSocketError(callback)
下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。
参数
OnSocketErrorCallbackResult 的属性值
名称 | 类型 | 必备 | 默认值 | 描述 |
errMsg | string | 是 | - | 错误信息 |
onSocketError 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
相关 Bug
uni.sendSocketMessage(options)
通过 WebSocket 连接发送数据,需要先 uni.connectSocket,并在 uni.onSocketOpen 回调之后才能发送。
参数
SendSocketMessageFail 的属性值
名称 | 类型 | 必备 | 默认值 | 描述 |
errCode | 10001 | 10002 | 602001 | 是 | - | 错误码 - 10001 发送数据超限,发送队列不能超过16M大小。 - 10002 websocket未连接 - 602001 websocket系统错误 |
errSubject | string | 是 | - | 统一错误主题(模块)名称 |
data | any | null | 否 | - | 错误信息中包含的数据 |
cause | Error | null | 否 | - | 源错误信息,可以包含多个错误,详见SourceError |
errMsg | string | 是 | - | - |
SendSocketMessageOptions 兼容性
| Android | iOS | web |
data | 3.9 | 4.11 | - |
sendSocketMessage 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
相关 Bug
注意事项
- 出于性能的权衡,在底层实现上发送队列占用的内存不能超过16M,一旦超过将导致连接被关闭。
uni.onSocketMessage(callback)
监听WebSocket接受到服务器的消息事件。
参数
OnSocketMessageCallbackResult 的属性值
名称 | 类型 | 必备 | 默认值 | 描述 |
data | any | 是 | - | 服务器返回的消息 |
OnSocketMessageCallbackResult 兼容性
| Android | iOS | web |
data | 3.9 | 4.11 | - |
onSocketMessage 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
相关 Bug
uni.closeSocket(options)
关闭 WebSocket 连接。
参数
名称 | 类型 | 必填 | 默认值 | 描述 |
options | CloseSocketOptions | 是 | - | |
名称 | 类型 | 必备 | 默认值 | 描述 | code | number | null | 否 | 1000 | 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。如果这个参数没有被指定,默认的取值是1000 (表示正常连接关闭) | reason | string | null | 否 | "" | 一个可读的字符串,表示连接被关闭的原因。这个字符串必须是不长于123字节的UTF-8 文本(不是字符) | success | (result: GeneralCallbackResult) => void | null | 否 | null | 接口调用成功的回调函数 | fail | (result: GeneralCallbackResult) => void | null | 否 | null | 接口调用失败的回调函数 | complete | (result: GeneralCallbackResult) => void | null | 否 | null | 接口调用结束的回调函数(调用成功、失败都会执行) |
|
CloseSocketOptions 兼容性
| Android | iOS | web |
code | 3.9 | 4.11 | - |
reason | 3.9 | 4.11 | - |
closeSocket 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
相关 Bug
uni.onSocketClose(callback)
监听WebSocket关闭。
参数
OnSocketCloseCallbackResult 的属性值
名称 | 类型 | 必备 | 默认值 | 描述 |
code | number | 是 | - | 一个数字值表示关闭连接的状态号,表示连接被关闭的原因。 |
reason | string | 是 | - | 一个可读的字符串,表示连接被关闭的原因。 |
OnSocketCloseCallbackResult 兼容性
| Android | iOS | web |
code | 3.9 | 4.11 | - |
reason | 3.9 | 4.11 | - |
onSocketClose 兼容性
Android | iOS | web |
3.9 | 4.11 | 4.0 |
参见
相关 Bug
示例
hello uni-app x
通用类型
GeneralCallbackResult
名称 | 类型 | 必备 | 默认值 | 描述 |
errMsg | string | 是 | - | 错误信息 |