-
Notifications
You must be signed in to change notification settings - Fork 157
Description
Error: publish failed, errorCode: -10002, due to Preform operation without login RTM service.
`import 'package:agora_rtm/agora_rtm.dart';
import 'package:flutter/material.dart';
import 'dart:convert';
void main() {
// Ensure Flutter bindings are initialized
WidgetsFlutterBinding.ensureInitialized();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@OverRide
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@OverRide
State createState() => _HomePageState();
}
class _HomePageState extends State {
final String userId = 'madhu';
final String appId = '143....................783f1ec7e857efd';
final String token ="007eJxTYOjYE1S/.........................ydLM3MTUIMkg0QIAJRsmDA==";
final String channelName = 'madhu_channel';
RtmClient? rtmClient;
@OverRide
void initState() {
super.initState();
createClient();
}
@OverRide
void dispose() {
rtmClient?.logout();
super.dispose();
}
Future createClient() async {
try {
// Create RTM client
final statusAndClient = await RTM(appId, userId);
final status = statusAndClient.$1;
final client = statusAndClient.$2;
if (status.error == true) {
print(
'${status.operation} failed due to ${status.reason}, error code: ${status.errorCode}');
} else {
rtmClient = client;
print('Initialize success!');
}
// Add event listeners
rtmClient!.addListener(
// Message event handler
message: (event) {
print(
'Received a message from channel: ${event.channelName}, channel type: ${event.channelType}');
print(
'Message content: ${utf8.decode(event.message!)}, custom type: ${event.customType}');
},
// Link state event handler
linkState: (event) {
print(
'Link state changed from ${event.previousState} to ${event.currentState}');
print('Reason: ${event.reason}, due to operation ${event.operation}');
},
);
// Login to RTM
await login(rtmClient!, token);
await sendMessage(rtmClient!, channelName);
} catch (e) {
print('Initialization failed: $e');
}
}
Future login(RtmClient client, String token) async {
try {
// Login to RTM
final statusAndResponse = await client.login(token);
final status = statusAndResponse.$1;
if (status.error == true) {
print(
'------------------------- ${status.operation} failed due to ${status.reason}, error code: ${status.errorCode}');
} else {
print('Login to RTM successful!');
await joinChannel(rtmClient!, channelName);
}
} catch (e) {
print('Failed to login: $e');
}
}
Future sendMessage(RtmClient client, String channelName) async {
// Send a message every second for 10 seconds
for (var i = 0; i < 10; i++) {
try {
final statusAndResponse = await client.publish(
channelName,
'Message number: $i',
channelType: RtmChannelType.message,
customType: 'PlainText',
);
final status = statusAndResponse.$1;
if (status.error == true) {
print(
'${status.operation} failed, errorCode: ${status.errorCode}, due to ${status.reason}');
} else {
print('${status.operation} success! Message number: $i');
}
} catch (e) {
print('Failed to publish message: $e');
}
await Future.delayed(const Duration(seconds: 1));
}
}
Future joinChannel(RtmClient client, String channelName) async {
try {
// Subscribe to a channel
final statusAndResponse = await client.subscribe(channelName);
final status = statusAndResponse.$1;
if (status.error == true) {
print(
'${status.operation} failed due to ${status.reason}, error code: ${status.errorCode}');
} else {
print('Subscribed to channel: $channelName successfully!');
}
} catch (e) {
print('Failed to subscribe to channel: $e');
}
}
@OverRide
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
actions: [
IconButton(onPressed: ()async{
createClient();
}, icon: Icon(Icons.refresh))
],
),
body: Center(
child: Text("Welcome"),
),
);
}
}
`