Compare commits

..

6 Commits

Author SHA1 Message Date
b53c62b40c Merge pull request 'fix: make the moxxmpp example work again' (#29) from bleonard252/moxxmpp-patch:master into master
Reviewed-on: https://codeberg.org/moxxy/moxxmpp/pulls/29
2023-03-10 23:07:49 +00:00
Blake Leonard
2cdc56c882
chore: format corrections, comment clarifications
Signed-off-by: Blake Leonard <me@blakes.dev>
2023-03-10 13:29:30 -05:00
Blake Leonard
f5059d8008 Merge remote-tracking branch 'origin/master' into HEAD 2023-03-08 15:08:56 -05:00
Blake Leonard
792ec4d731
chore(example): format and fix lint errors
Signed-off-by: Blake Leonard <me@blakes.dev>
2023-03-08 15:07:55 -05:00
Blake Leonard
60c89e28d3
chore(example): switch to connectAwaitable
That way it only acts connected when the credentials have been accepted.

Also I had to correct the value of "allowPlainAuth" which should be
true since a bug with it has been identified, and not yet fixed.

Signed-off-by: Blake Leonard <me@blakes.dev>
2023-03-08 14:09:37 -05:00
Blake Leonard
7b215d5c6e
fix: make the moxxmpp example work again
Note: to do this, I could not use the ExampleTcpSocketWrapper.
If I did, the app crashed on launch.

I also added some functionality: the header bar turns green when
connected, the FAB says what it does, and you can disconnect.

Signed-off-by: Blake Leonard <me@blakes.dev>
2023-03-08 13:02:43 -05:00

View File

@ -11,12 +11,14 @@ class ExampleTcpSocketWrapper extends TCPSocketWrapper {
Future<List<MoxSrvRecord>> srvQuery(String domain, bool dnssec) async { Future<List<MoxSrvRecord>> srvQuery(String domain, bool dnssec) async {
final records = await MoxdnsPlugin.srvQuery(domain, false); final records = await MoxdnsPlugin.srvQuery(domain, false);
return records return records
.map((record) => MoxSrvRecord( .map(
(record) => MoxSrvRecord(
record.priority, record.priority,
record.weight, record.weight,
record.target, record.target,
record.port, record.port,
),) ),
)
.toList(); .toList();
} }
} }
@ -24,6 +26,7 @@ class ExampleTcpSocketWrapper extends TCPSocketWrapper {
void main() { void main() {
Logger.root.level = Level.ALL; Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) { Logger.root.onRecord.listen((record) {
// ignore: avoid_print
print('${record.level.name}: ${record.time}: ${record.message}'); print('${record.level.name}: ${record.time}: ${record.message}');
}); });
@ -54,22 +57,29 @@ class MyHomePage extends StatefulWidget {
} }
class _MyHomePageState extends State<MyHomePage> { class _MyHomePageState extends State<MyHomePage> {
final logger = Logger('MyHomePage');
final XmppConnection connection = XmppConnection( final XmppConnection connection = XmppConnection(
ExponentialBackoffReconnectionPolicy(), RandomBackoffReconnectionPolicy(1, 60),
ExampleTcpSocketWrapper(), AlwaysConnectedConnectivityManager(),
// The below causes the app to crash.
//ExampleTcpSocketWrapper(),
// In a production app, the below should be false.
TCPSocketWrapper(true),
); );
TextEditingController jidController = TextEditingController(); TextEditingController jidController = TextEditingController();
TextEditingController passwordController = TextEditingController(); TextEditingController passwordController = TextEditingController();
bool connected = false;
bool loading = false;
_MyHomePageState() : super() { _MyHomePageState() : super() {
connection connection
..registerManagers([ ..registerManagers([
StreamManagementManager(), StreamManagementManager(),
DiscoManager(), DiscoManager([]),
RosterManager(), RosterManager(TestingRosterStateManager("", [])),
PingManager(), PingManager(),
MessageManager(), MessageManager(),
PresenceManager('http://moxxmpp.example'), PresenceManager(),
]) ])
..registerFeatureNegotiators([ ..registerFeatureNegotiators([
ResourceBindingNegotiator(), ResourceBindingNegotiator(),
@ -85,15 +95,45 @@ class _MyHomePageState extends State<MyHomePage> {
} }
Future<void> _buttonPressed() async { Future<void> _buttonPressed() async {
if (connected) {
await connection.disconnect();
setState(() {
connected = false;
});
return;
}
setState(() {
loading = true;
});
connection.setConnectionSettings( connection.setConnectionSettings(
ConnectionSettings( ConnectionSettings(
jid: JID.fromString(jidController.text), jid: JID.fromString(jidController.text),
password: passwordController.text, password: passwordController.text,
useDirectTLS: true, useDirectTLS: true,
allowPlainAuth: false, // If `allowPlainAuth` is `false`, connecting to some
// servers will cause apps to hang, and never connect.
// The hang is a bug that will be fixed, so when it is,
// allowPlainAuth should be set to false.
allowPlainAuth: true,
), ),
); );
await connection.connect(); final result = await connection.connectAwaitable();
setState(() {
connected = result.success;
loading = false;
});
if (result.error != null) {
logger.severe(result.error);
if (context.mounted) {
showDialog(
context: context,
builder: (_) => AlertDialog(
title: const Text('Error'),
content: Text(result.error.toString()),
),
);
}
}
} }
@override @override
@ -101,20 +141,24 @@ class _MyHomePageState extends State<MyHomePage> {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(widget.title), title: Text(widget.title),
backgroundColor: connected ? Colors.green : Colors.deepPurple[800],
foregroundColor: connected ? Colors.black : Colors.white,
), ),
body: Center( body: Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
TextField( TextField(
enabled: !loading,
controller: jidController, controller: jidController,
decoration: InputDecoration( decoration: const InputDecoration(
labelText: 'JID', labelText: 'JID',
), ),
), ),
TextField( TextField(
enabled: !loading,
controller: passwordController, controller: passwordController,
decoration: InputDecoration( decoration: const InputDecoration(
labelText: 'Password', labelText: 'Password',
), ),
obscureText: true, obscureText: true,
@ -122,10 +166,13 @@ class _MyHomePageState extends State<MyHomePage> {
], ],
), ),
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton.extended(
onPressed: _buttonPressed, onPressed: _buttonPressed,
label: Text(connected ? 'Disconnect' : 'Connect'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
tooltip: 'Connect', tooltip: 'Connect',
child: const Icon(Icons.add), icon: const Icon(Icons.power),
), ),
); );
} }