subscribe()
subscribe(callback?: (status: REALTIME_SUBSCRIBE_STATES, err?: Error) => void, timeout?: number): RealtimeChannel
向服务端注册频道。未连接时会自动 connect()。
status | 含义 |
|---|---|
SUBSCRIBED | 订阅成功 |
CHANNEL_ERROR | 订阅失败,查看 err(含 cause) |
TIMED_OUT | 服务端超时未响应 |
CLOSED | 频道被关闭 |
参数
callback
(status, err?) => void
订阅状态回调
timeout
number
超时时间(ms)
返回
RealtimeChannel
RealtimeChannel
当前频道,便于链式调用
示例
channel.subscribe((status, err) => {
if (status === "CHANNEL_ERROR" || status === "TIMED_OUT") {
console.error(status, err);
}
});