joinChannel method

void joinChannel(
  1. String channel,
  2. Map<String, dynamic> param,
  3. String type
)

Implementation

void joinChannel(String channel, Map<String, dynamic> param, String type) {
  final validConnection = _connection;
  if (validConnection == null) {
    Log().error("joinChannel $channel validConnection == null");
    return;
  }
  if (validConnection.isConnected == false) {
    Log().error("joinChannel $channel validConnection.isConnected == false");
    return;
  }
  _removeChannelWithKey(type);
  if (type == "roomChannel" || type == "donationChannel") {
    _presence = null;
  }
  final newChannel = validConnection.channel(channel, param);
  _addChannelWithKey(type, newChannel);

  try {
    newChannel.join()?.receive("ok", (response) => {
      if (type == "callChannel") {
        onCallChannelJoin?.call(this, response)
      } else if (type == "lobbyChannel") {
        onLobbyChannelJoin?.call(this,response)
      }
    }).receive("error", (response) => {
      onCallChannelError?.call(this,response)
    });
  } on String catch (error) {
    Log().error("joinChannel channel = $channel, error = $error");
  }

  if (type == "callChannel") {
    _presence = PhoenixPresence(newChannel);
    listBy(iden, pres) {
      var first = pres["metas"].first;
      first["count"] = pres["metas"]!.length;
      first["id"] = iden;
      return first;
    }

    // _presence?.onJoin((key,currentPresence,newPresence) {
    //   Log().info("callChannel onJoin newPresence = $newPresence");
    // });
    _presence?.onSync(() {
      Log().info("callChannel onSync");
      _notifyListChange(_presence?.list(by: listBy));
    });
    newChannel.onClose((payload, ref, joinRef) {

    });
    newChannel.on("message", (payload, ref, joinRef) {
      final validPayload = payload;
      if (validPayload != null && validPayload is Map<String,dynamic>) {
        Future.delayed(const Duration(milliseconds: 0), () {
          onCallChannelMessage?.call(this, validPayload, "message");
        });
      }
    });
    newChannel.on("1-1", (payload, ref, joinRef) {
      final validPayload = payload;
      if (validPayload != null && validPayload is Map<String,dynamic>) {
        Future.delayed(const Duration(milliseconds: 0), () {
          onCallChannelMessage?.call(this, validPayload, "1-1");
        });
      }
    });
  }
  else if (type == "lobbyChannel") {
    newChannel.onClose((payload, ref, joinRef) {

    });
    newChannel.on("message", (payload, ref, joinRef) {
      final validPayload = payload;
      if (validPayload != null && validPayload is Map<String,dynamic>) {
        Future.delayed(const Duration(milliseconds: 0), () {
          onLobbyChannelMessage?.call(this, validPayload, "message");
        });
      }
    });
  }
}