Skip to main content

WebSocket

WebSocket 类表示页面中的 websocket 连接。

webSocket.on('close')

Added in: v1.8

当 WebSocket 关闭时触发。

webSocket.on('framereceived')

Added in: v1.9

当 WebSocket 接收到一帧数据时触发。

webSocket.on('framesent')

Added in: v1.9

当 WebSocket 发送一帧数据时触发。

webSocket.on('socketerror')

Added in: v1.9

当 WebSocket 发生错误时触发。

webSocket.isClosed()

Added in: v1.8

指示该 WebSocket 是否已经关闭。

webSocket.url()

Added in: v1.8

包含 WebSocket 的 URL。

webSocket.waitForEvent(event[, optionsOrPredicate, options])

Added in: v1.8
  • event <string> 事件名称,与传给 webSocket.on(event) 的一致。#
  • optionsOrPredicate? <function|Object> 可选,接收事件数据的谓词函数或一个 options 对象。#
  • options? <Object>
    • predicate? <function> 接收事件数据,当返回 truthy 时结束等待。#
  • returns: <Promise<Object>>#

等待某个事件触发,并将其值传给谓词函数。当谓词返回 truthy 时结束等待。如果 WebSocket 在事件触发之前关闭,会抛出错误。返回事件数据。