moxxmpp/example_flutter/lib/main.dart

218 lines
6.9 KiB
Dart
Raw Normal View History

2022-11-08 16:54:19 +00:00
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:moxdns/moxdns.dart';
import 'package:moxxmpp/moxxmpp.dart';
2022-11-08 19:05:22 +00:00
import 'package:moxxmpp_socket_tcp/moxxmpp_socket_tcp.dart';
2022-11-08 16:54:19 +00:00
class ExampleTcpSocketWrapper extends TCPSocketWrapper {
ExampleTcpSocketWrapper() : super();
2022-11-08 16:54:19 +00:00
@override
Future<List<MoxSrvRecord>> srvQuery(String domain, bool dnssec) async {
final records = await MoxdnsPlugin.srvQuery(domain, false);
return records
.map(
(record) => MoxSrvRecord(
record.priority,
record.weight,
record.target,
record.port,
),
)
.toList();
2022-11-08 16:54:19 +00:00
}
}
void main() {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) {
// ignore: avoid_print
2022-11-08 16:54:19 +00:00
print('${record.level.name}: ${record.time}: ${record.message}');
});
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Moxxmpp Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(title: 'Moxxmpp Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final logger = Logger('MyHomePage');
2022-11-08 16:54:19 +00:00
final XmppConnection connection = XmppConnection(
RandomBackoffReconnectionPolicy(1, 60),
AlwaysConnectedConnectivityManager(),
ClientToServerNegotiator(),
// The below causes the app to crash.
//ExampleTcpSocketWrapper(),
// In a production app, the below should be false.
TCPSocketWrapper(),
2022-11-08 16:54:19 +00:00
);
TextEditingController jidController = TextEditingController();
TextEditingController passwordController = TextEditingController();
bool connected = false;
bool loading = false;
2022-11-08 16:54:19 +00:00
_MyHomePageState() : super() {
connection
..registerManagers([
StreamManagementManager(),
DiscoManager([]),
RosterManager(TestingRosterStateManager("", [])),
PingManager(
const Duration(minutes: 3),
),
2022-11-08 16:54:19 +00:00
MessageManager(),
PresenceManager(),
OccupantIdManager(),
MUCManager()
2022-11-08 16:54:19 +00:00
])
..registerFeatureNegotiators([
ResourceBindingNegotiator(),
StartTlsNegotiator(),
StreamManagementNegotiator(),
CSINegotiator(),
RosterFeatureNegotiator(),
SaslPlainNegotiator(),
SaslScramNegotiator(10, '', '', ScramHashType.sha512),
2022-11-08 16:54:19 +00:00
SaslScramNegotiator(9, '', '', ScramHashType.sha256),
SaslScramNegotiator(8, '', '', ScramHashType.sha1),
]);
}
2022-11-08 16:54:19 +00:00
Future<void> _buttonPressed() async {
if (connected) {
await connection.disconnect();
setState(() {
connected = false;
});
return;
}
setState(() {
loading = true;
});
connection.connectionSettings = ConnectionSettings(
jid: JID.fromString(jidController.text),
password: passwordController.text,
2022-11-08 16:54:19 +00:00
);
final result = await connection.connect(waitUntilLogin: true);
setState(() {
connected = result.isType<bool>() && result.get<bool>();
loading = false;
});
2023-03-18 13:59:47 +00:00
if (result.isType<XmppError>()) {
logger.severe(result.get<XmppError>());
if (context.mounted) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text('Error'),
2023-03-18 13:59:47 +00:00
content: Text(result.get<XmppError>().toString()),
),
);
}
}
2022-11-08 16:54:19 +00:00
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
backgroundColor: connected ? Colors.green : Colors.deepPurple[800],
foregroundColor: connected ? Colors.black : Colors.white,
2022-11-08 16:54:19 +00:00
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
enabled: !loading,
2022-11-08 16:54:19 +00:00
controller: jidController,
decoration: const InputDecoration(
2022-11-08 16:54:19 +00:00
labelText: 'JID',
),
),
TextField(
enabled: !loading,
2022-11-08 16:54:19 +00:00
controller: passwordController,
decoration: const InputDecoration(
2022-11-08 16:54:19 +00:00
labelText: 'Password',
),
obscureText: true,
),
TextButton(
onPressed: () async {
// final muc = connection.getManagerById<MUCManager>(mucManager);
// final roomInformationResult = await muc!.queryRoomInformation(
// JID.fromString('moxxmpp-muc-test@muc.moxxy.org'));
// if (roomInformationResult.isType<RoomInformation>()) {
// print('Room information received');
// print(roomInformationResult.get<RoomInformation>().jid);
// print(roomInformationResult.get<RoomInformation>().name);
// print(roomInformationResult.get<RoomInformation>().features);
// }
// final muc = connection.getManagerById<MUCManager>(mucManager);
// print('joining room');
// final roomInformationResult = await muc!.joinRoom(
// JID.fromString('moxxmpp-muc-test@muc.moxxy.org/test_1'));
// if (roomInformationResult.isType<MUCError>()) {
// print(roomInformationResult.get());
// } else {
// print(roomInformationResult.get());
// }
print('HERE IS YOUR JID');
print(connection.resource);
final sid = connection.generateId();
final originId = connection.generateId();
final message =
connection.getManagerById<MessageManager>(messageManager);
message!.sendMessage(
JID.fromString('moxxmpp-muc-test@muc.moxxy.org/ISD'),
TypedMap<StanzaHandlerExtension>.fromList([
const MessageBodyData('Testing'),
const MarkableData(true),
MessageIdData(sid),
StableIdData(originId, null),
ConversationTypeData(ConversationType.groupchatprivate)
]),
);
},
child: const Text('Test'),
),
2022-11-08 16:54:19 +00:00
],
),
),
floatingActionButton: FloatingActionButton.extended(
2022-11-08 16:54:19 +00:00
onPressed: _buttonPressed,
label: Text(connected ? 'Disconnect' : 'Connect'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
2022-11-08 16:54:19 +00:00
tooltip: 'Connect',
icon: const Icon(Icons.power),
2022-11-08 16:54:19 +00:00
),
);
}
}