i'm trying to join a channel but i get PlatformException(-17, request to join channel is rejected, null, null)
Asked Answered
E

4

5

I'm creating a channel from agora web demo as host and trying to join the channel from the flutter app as an audience i made sure both use the same channel name, app id, and I'm generating tokens through a server and Fech them to the app.

here's my code:


import 'package:agora_rtc_engine/rtc_engine.dart';
import 'package:agora_rtc_engine/rtc_remote_view.dart' as rtc_remote_view;
import 'package:agora_rtm/agora_rtm.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

import '../../utilities/agora_app_id.dart';

class StreamScreen extends StatefulWidget {
  static const routeName = '/stream';
  final String channelName;
  final String userName;
  final int userID;

  const StreamScreen({
    Key? key,
    required this.channelName,
    required this.userName,
    required this.userID,
  }) : super(key: key);

  @override
  State<StreamScreen> createState() => _StreamScreenState();
}

class _StreamScreenState extends State<StreamScreen> {
  final List<int> _audience = [];
  late RtcEngine _rtcEngine;
  AgoraRtmClient? _rtmClient;
  AgoraRtmChannel? _rtmChannel;
  String? _rtmToken;
  String? _rtcToken;

  Future<void> initAgora() async {
    _rtcEngine =
        await RtcEngine.createWithContext(RtcEngineContext(agoraAppID));
    _rtmClient = await AgoraRtmClient.createInstance(agoraAppID);

    await getRTMToken();

    await _rtcEngine.enableVideo();
    await _rtcEngine.startPreview();
    await _rtcEngine.setChannelProfile(ChannelProfile.LiveBroadcasting);
    await _rtcEngine.setClientRole(ClientRole.Audience);
    await getRTCToken();

    //join the RTC & RTM Channels

    await _rtcEngine.joinChannel(
      _rtcToken,
      widget.channelName,
      null,
      0,
    );

    //event handlers for RTC Engine
    _rtcEngine.setEventHandler(RtcEngineEventHandler(
      joinChannelSuccess: (channel, uid, elapsed) {
        //TODO: add joinChannel logic
        print('joinChannelSuccess $channel $uid');
      },
      userJoined: (uid, elapsed) {
        print('User $uid joined');
      },
      leaveChannel: (rtcStats) {
        setState(() {
          _audience.clear();
        });
      },
      tokenPrivilegeWillExpire: (rtcToken) async {
        await getRTCToken();
        await _rtcEngine.renewToken(rtcToken);
      },
    ));

    //event handlers for RTM Client
    _rtmClient?.onConnectionStateChanged = (state, reason) {
      print('Connection State changed: ' +
          state.toString() +
          ', reason: ' +
          reason.toString());
      if (state == 5) {
        _rtmChannel?.leave();
        _rtmClient?.logout();
        _rtmClient?.destroy();
        print('logged out.');
      }
    };

    //event handlers for RTM Channel
    _rtmChannel?.onMemberJoined = (member) {
      print('member joined: ' + member.userId + 'channel: ' + member.channelId);
    };

    _rtmChannel?.onMemberLeft = (member) {
      print('member left: ' + member.userId + 'channel: ' + member.channelId);
    };
  }

  @override
  void initState() {
    super.initState();
    initAgora();
  }

  @override
  void dispose() {
    _audience.clear();
    _rtcEngine.leaveChannel();
    _rtcEngine.destroy();
    _rtmChannel?.leave();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: rtc_remote_view.SurfaceView(
          channelId: widget.channelName,
          uid: widget.userID,
          zOrderOnTop: true,
          zOrderMediaOverlay: true,
        ),
        // const Toolbar(),
      ),
    );
  }

  Future<void> getRTMToken() async {
    final response = await http.get(
      Uri.parse(
          'https://*********************.com/rtm/${widget.userID}/?expiry=7000'),
    );

    if (response.statusCode == 200) {
      setState(() {
        _rtmToken = response.body;
        _rtmToken = jsonDecode(_rtmToken!)['rtmToken'];
      });
      print('rtm: ' + _rtmToken!);
    } else {
      print('Failed to fetch the token');
    }
  }

  Future<void> getRTCToken() async {
    final response = await http.get(
      Uri.parse(
          'https://******************.com/rtc/${widget.channelName}/audience/uid/${0}/?expiry=7000/'),
    );

    if (response.statusCode == 200) {
      setState(() {
        _rtcToken = response.body;
        _rtcToken = jsonDecode(_rtcToken!)['rtcToken'];
      });
      print('rtc: ' + _rtcToken!);
    } else {
      print('Failed to fetch the token');
    }
  }
}

i get this error:

#0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177:18)
<asynchronous suspension>
#2      _StreamScreenState.initAgora (package:malhamti_app/screens/stream/stream_screen.dart:58:5)
<asynchronous suspension>
  • what am i doing wrong?
  • do i need to set up the RTM channel/client, when I'm just wanting video streaming?
  • What does the number -17 in the error mean?
Edi answered 30/4, 2022 at 12:27 Comment(0)
W
7

Error code 17 occurs when a user is already in the channel and you try to join the channel again. Make sure that you call leaveChannel() method before leaving the channel.

Weakfish answered 31/5, 2022 at 16:23 Comment(0)
P
2

Error code 17 happen when you are in test account in agora to solve this exception you must change account from test to live

check image from agora console

Pyxidium answered 21/9, 2022 at 21:48 Comment(1)
Error code 17 is not related to Live or Test state. docs.agora.io/en/voice/API%20Reference/java/… Relevant bits: The request to join the channel is rejected. This error usually occurs when: The user is already in the channel, and still calls the API to join the channel, for example, joinChannel. The user tries to join the channel during echo test. Wait until the echo test is finished. The user tries to join the channel with a token that is expired.Nutritionist
M
0

if some one is having this issue while using this agora UI Kit. https://pub.dev/packages/agora_uikit

You should leave the channel after initialising the agora UI Kit. As shown below

AgoraClient client = // Intialization stuff goes here...
client.initialize();
client.engine.leaveChannel();
Maisey answered 1/9, 2023 at 20:38 Comment(0)
T
0

before joinchannel methode just call this

    await agoraEngine.leaveChannel();
Tannertannery answered 27/9, 2023 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.