# 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

# SocketTask 的方法

# send(options)

通过 WebSocket 连接发送数据

# 参数
名称 类型 必填 默认值 描述
options SendSocketMessageOptions -
名称 类型 必备 默认值 描述
data any - 需要发送的内容
success (result: GeneralCallbackResult) => void | null null 接口调用成功的回调函数
fail (result: SendSocketMessageFail) => void | null null 接口调用失败的回调函数
complete (result: any) => void | null null 接口调用结束的回调函数(调用成功、失败都会执行)
# 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 连接打开事件

# 参数
名称 类型 必填 默认值 描述
callback OnSocketOpenCallbackResult -
名称 类型 必备 默认值 描述
header any - 连接成功的 HTTP 响应 Header
# 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 错误

# 参数
名称 类型 必填 默认值 描述
callback (result: GeneralCallbackResult) => void -
# onError 兼容性
Android iOS web
3.9 4.11 4.0
# 参见

# onMessage(callback)

监听 WebSocket 接受到服务器的消息事件

# 参数
名称 类型 必填 默认值 描述
callback (result: OnSocketMessageCallbackResult) => void -
# 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连接打开事件。

# 参数

名称 类型 必填 默认值 描述
options (result: OnSocketOpenCallbackResult) => void -

# onSocketOpen 兼容性

Android iOS web
3.9 4.11 4.0

# 参见

相关 Bug

# uni.onSocketError(callback)

下载文件资源到本地,客户端直接发起一个 HTTP GET 请求,返回文件的本地临时路径。

# 参数

名称 类型 必填 默认值 描述
callback (result: OnSocketErrorCallbackResult) => void -

# OnSocketErrorCallbackResult 的属性值

名称 类型 必备 默认值 描述
errMsg string - 错误信息

# onSocketError 兼容性

Android iOS web
3.9 4.11 4.0

# 参见

相关 Bug

# uni.sendSocketMessage(options)

通过 WebSocket 连接发送数据,需要先 uni.connectSocket,并在 uni.onSocketOpen 回调之后才能发送。

# 参数

名称 类型 必填 默认值 描述
options SendSocketMessageOptions -
名称 类型 必备 默认值 描述
data any - 需要发送的内容
success (result: GeneralCallbackResult) => void | null null 接口调用成功的回调函数
fail (result: SendSocketMessageFail) => void | null null 接口调用失败的回调函数
complete (result: any) => void | null null 接口调用结束的回调函数(调用成功、失败都会执行)
# 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接受到服务器的消息事件。

# 参数

名称 类型 必填 默认值 描述
callback (result: OnSocketMessageCallbackResult) => void -

# 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关闭。

# 参数

名称 类型 必填 默认值 描述
callback (result: OnSocketCloseCallbackResult) => void -

# 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

Template

Script

<template>
  <page-head title="websocket通讯示例"></page-head>
  <view class="uni-padding-wrap">
    <view class="uni-btn-v">
      <text class="websocket-msg">{{ showMsg }}</text>
      <button class="uni-btn-v" type="primary" @click="connect">
        连接websocket服务
      </button>
      <button class="uni-btn-v" v-show="connected" type="primary" @click="send">
        发送一条消息
      </button>
      <button class="uni-btn-v" type="primary" @click="close">
        断开websocket服务
      </button>
      <text class="websocket-tips">发送消息后会收到一条服务器返回的消息(与发送的消息内容一致)</text>
    </view>
  </view>
</template>



<style>
  .uni-btn-v {
    padding: 5px 0;
  }

  .uni-btn-v {
    margin: 10px 0;
  }

  .websocket-msg {
    padding: 40px 0px;
    text-align: center;
    font-size: 14px;
    line-height: 40px;
    color: #666666;
  }

  .websocket-tips {
    padding: 40px 0px;
    text-align: center;
    font-size: 14px;
    line-height: 24px;
    color: #666666;
  }
</style>

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 描述
errMsg string - 错误信息