Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Im getting this error AgoraRtmClient login failed errorCode: 5 when i tries to login #166

Open
21prnv opened this issue May 7, 2024 · 0 comments

Comments

@21prnv
Copy link

21prnv commented May 7, 2024

here is the code

`import 'package:flutter/material.dart';
import 'dart:async';

import 'package:agora_rtm/agora_rtm.dart';
import 'package:rtm/logs.dart';
import 'package:rtm/message.dart';

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
@OverRide
_MyAppState createState() => _MyAppState();
}

class _MyAppState extends State {
final _userId = TextEditingController();
final _channelName = TextEditingController();

late AgoraRtmClient _client;
late AgoraRtmChannel _channel;
LogController logController = LogController();

@OverRide
void initState() {
super.initState();
_createClient();
}

void _createClient() async {
_client =
await AgoraRtmClient.createInstance('AppId');
_client.onMessageReceived = (AgoraRtmMessage message, String peerId) {
logController
.addLog("Private Message from " + peerId + ": " + message.text);
};
_client.onConnectionStateChanged = (int state, int reason) {
logController.addLog('Connection state changed: ' +
state.toString() +
', reason: ' +
reason.toString());
if (state == 5) {
_client.logout();
logController.addLog('Logout.');
}
};
}

void _login(BuildContext context) async {
String userId = _userId.text;
if (userId.isEmpty) {
print('Please input your user id to login.');
return;
}

try {
  await _client.login(null, userId);
  logController.addLog('Login success: ' + userId);
  _joinChannel(context);
} on AgoraRtmClientException catch (errorCode) {
  print('Login error: ' + errorCode.reason);
}

}

void _joinChannel(BuildContext context) async {
String channelId = _channelName.text;
if (channelId.isEmpty) {
logController.addLog('Please input channel id to join.');
return;
}

try {
  _channel = await _createChannel(channelId);
  await _channel.join();
  logController.addLog('Join channel success.');
  Navigator.push(
      context,
      MaterialPageRoute(
          builder: (context) => MessageScreen(
                client: _client,
                channel: _channel,
                logController: logController,
              )));
} catch (errorCode) {
  print('Join channel error: ' + errorCode.toString());
}

}

Future _createChannel(String name) async {
AgoraRtmChannel? channel = await _client.createChannel(name);
channel!.onMemberJoined = (AgoraRtmMember member) {
logController.addLog(
"Member joined: " + member.userId + ', channel: ' + member.channelId);
};
channel.onMemberLeft = (AgoraRtmMember member) {
logController.addLog(
"Member left: " + member.userId + ', channel: ' + member.channelId);
};
channel.onMessageReceived =
(AgoraRtmMessage message, AgoraRtmMember member) {
logController
.addLog("Public Message from " + member.userId + ": " + message.text);
};
return channel;
}

@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Agora Real Time Message'),
),
body: Container(
padding: const EdgeInsets.all(16),
child: Column(
children: [
TextField(
controller: _userId,
decoration: InputDecoration(hintText: 'User ID')),
TextField(
controller: _channelName,
decoration: InputDecoration(hintText: 'Channel')),
OutlinedButton(
child: Text('Login'), onPressed: () => _login(context)),
],
),
),
);
}
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant