flutter_xmpp 0.0.14 flutter_xmpp: ^0.0.14 copied to clipboard
Flutter XMPP for Realtime Communication, this plugin use Smack Library on Android to connect with XMPP server,currently this plugin still development and will be update ...
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'package:flutter_xmpp/flutter_xmpp.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String rerceiveMessageFrom = '';
String rerceiveMessageBody = '';
FlutterXmpp flutterXmpp;
@override
void initState() {
super.initState();
initXmpp();
}
@override
void dispose() async{
await flutterXmpp.stop();
super.dispose();
}
Future<void> initXmpp() async{
var auth = {
"user_jid": "papua-dev@0.0.0.0/Android",
"password":"papua-dev",
"host":"192.168.101.105",
"port":5222
};
flutterXmpp = new FlutterXmpp(auth);
await flutterXmpp.start(_onReceiveMessage,_onError);
sleep(const Duration(seconds:2));
print(await flutterXmpp.currentState()); // get current state
await flutterXmpp.sendMessage("xbon@0.0.0.0","Test Kirim Pesan Darii Android");
cycle();
}
void cycle() async{
SystemChannels.lifecycle.setMessageHandler((msg) async{
if(msg == "AppLifecycleState.inactive" || msg == "AppLifecycleState.suspending" ){
await flutterXmpp.stop();
}else if(msg == "AppLifecycleState.resumed"){
await flutterXmpp.start(_onReceiveMessage, _onError);
}
print('SystemChannels> $msg');
return "Lifecycle";
});
}
void _onReceiveMessage(dynamic event) {
print(event);
setState(() {
rerceiveMessageFrom = event['from'];
rerceiveMessageBody = event['body'];
});
}
void _onError(Object error) {
print(error);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('FlutterXMPP'),
),
body: Center(
child: Text('Status Login: \n$rerceiveMessageFrom\n$rerceiveMessageBody'),
),
),
);
}
}