join method

Future<JsonMap> join(
  1. String roomId,
  2. String? ext,
  3. bool leaveOthers
)

chatroom.join:leaveOthers 时服务端离开这条连接上的其他聊天室(Flutter 中都属于本进程)。进入被限流时按等待时间重试。

Implementation

Future<JsonMap> join(String roomId, String? ext, bool leaveOthers) async {
  await _waitConnected(_connectWaitMs);
  final data = <String, Object?>{'room_id': roomId, 'ext': ?ext};
  if (leaveOthers && _joined.any((id) => id != roomId)) data['leave_others'] = true;
  JsonMap reply;
  for (;;) {
    try {
      reply = asMap(await core.conn.request<Object?>('chatroom.join', data)) ?? <String, Object?>{};
      break;
    } on DRException catch (err) {
      final left = asList(err.details?['left_room_ids']);
      // 与 Web SDK 不同:错误中带 left_room_ids 的,这些聊天室的句柄同样结束(Web SDK 只从主标签页的登记中去掉)
      if (left != null) markLeft([for (final id in left) if (id is String && id != roomId) id]);
      if (err.code == LocalErrorCode.rateLimited && (err.reason == 'join_rate' || err.reason == 'room_join_rate') && (err.retryAfter ?? 61) <= 60) {
        await sleep(core.platform.clock, (err.retryAfter ?? 1) * 1000);
        continue;
      }
      rethrow;
    }
  }
  for (final id in asList(reply['left_room_ids']) ?? const []) {
    if (id is String) _joined.remove(id);
  }
  _joined.add(roomId);
  return reply;
}