Realtime

new Realtime(options)

Parameters:
Name Type Description
options Object
Properties
Name Type Attributes Default Description
appId String
appKey String (since 4.0.0)
server String | Object <optional>
指定服务器域名,中国节点应用此参数必填(since 4.0.0)
noBinary Boolean <optional>
false 设置 WebSocket 使用字符串格式收发消息(默认为二进制格式)。 适用于 WebSocket 实现不支持二进制数据格式的情况
ssl Boolean <optional>
true 使用 wss 进行连接
RTMServers String | Array.<String> <optional>
指定私有部署的 RTM 服务器地址(since 4.0.0)
plugins Array.<Plugin> <optional>
加载插件(since 3.1.0)

Extends

Members

constant register

注册消息类 在接收消息、查询消息时,会按照消息类注册顺序的逆序依次尝试解析消息内容

Methods

createIMClient(identityopt, optionsopt) → {Promise.<IMClient>} [async]

创建一个即时通讯客户端,多次创建相同 id 的客户端会返回同一个实例
Parameters:
Name Type Attributes Description
identity String | AV.User <optional>
客户端 identity,如果不指定该参数,服务端会随机生成一个字符串作为 identity, 如果传入一个已登录的 AV.User,则会使用该用户的 id 作为客户端 identity 登录。
options Object <optional>
Properties
Name Type Attributes Default Description
signatureFactory function <optional>
open session 时的签名方法 // TODO need details
conversationSignatureFactory function <optional>
对话创建、增减成员操作时的签名方法
blacklistSignatureFactory function <optional>
黑名单操作时的签名方法
tag String <optional>
客户端类型标记,以支持单点登录功能
isReconnect String <optional>
false 单点登录时标记该次登录是不是应用启动时自动重新登录
Returns:
Promise.<IMClient>

emit(event) → {Boolean}

依次调用 event 事件的监听器列表中的 listener。
Parameters:
Name Type Attributes Description
event String | Symbol The event name.
...arg Mixed <optional>
payloads
Returns:
Boolean - `true` if the event had listeners, else `false`.
Inherited From:

off(event, listeneropt, contextopt, onceopt) → {this}

移除 event 事件的监听器列表中的 listener。
Parameters:
Name Type Attributes Description
event String | Symbol The event name.
listener function <optional>
Only remove the listeners that match this function.
context Mixed <optional>
Only remove the listeners that have this context.
once Boolean <optional>
Only remove one-time listeners.
Returns:
this - self.
Inherited From:

on(event, listener, contextopt) → {this}

给指定的 event 添加监听器,并将该监听器置于监听器列表的末位。该方法不会检查是否已经添加过该监听器。重复添加相同的 event 和 listener 会导致该事件和监听器被重复触发。
Parameters:
Name Type Attributes Default Description
event String | Symbol The event name.
listener function The listener function.
context Mixed <optional>
this The context to invoke the listener with.
Returns:
this - self.
Inherited From:

once(event, listener, contextopt) → {this}

为 event 事件添加一个一次性的监听器,该事件第一次触发之后就会被注销。
Parameters:
Name Type Attributes Default Description
event String | Symbol The event name.
listener function The listener function.
context Mixed <optional>
this The context to invoke the listener with.
Returns:
this - self.
Inherited From:

pause()

暂停,使 SDK 进入离线状态。 你可以在网络断开、应用进入后台等时刻调用该方法让 SDK 进入离线状态,离线状态下不会尝试重连。 在浏览器中 SDK 会自动监听网络变化,因此无需手动调用该方法。
Since:
  • 3.4.0
See:

resume()

恢复在线状态。 你可以在网络恢复、应用回到前台等时刻调用该方法让 SDK 恢复在线状态,恢复在线状态后 SDK 会开始尝试重连。
Since:
  • 3.4.0
See:

retry()

手动进行重连。 SDK 在网络出现异常时会自动按照一定的时间间隔尝试重连,调用该方法会立即尝试重连并重置重连尝试计数器。 只能在 `SCHEDULE` 事件之后,`RETRY` 事件之前调用,如果当前网络正常或者正在进行重连,调用该方法会抛异常。

Events

DISCONNECT

连接断开。 连接断开可能是因为 SDK 进入了离线状态(see Realtime#event:OFFLINE),或长时间没有收到服务器心跳。 连接断开后所有的网络操作都会失败,请在连接断开后禁用相关的 UI 元素。

OFFLINE

客户端进入离线状态。 这通常意味着网络已断开,或者 Realtime#pause 被调用
Since:
  • 3.4.0

ONLINE

客户端恢复在线状态 这通常意味着网络已恢复,或者 Realtime#resume 被调用
Since:
  • 3.4.0

RECONNECT

连接恢复正常。 请重新启用在 Realtime#event:DISCONNECT 事件中禁用的相关 UI 元素

RETRY

正在尝试重新连接
Parameters:
Name Type Description
attempt Number 尝试重连的次数

SCHEDULE

计划在一段时间后尝试重新连接
Parameters:
Name Type Description
attempt Number 尝试重连的次数
delay Number 延迟的毫秒数