-
Notifications
You must be signed in to change notification settings - Fork 3
dynamic IP address for Meteor.connect(); #3
Comments
Meteor.connect has to be in the main function to be able to persist throughout the life of the app. But you can try to retrieve it from preferences and pass to Meteor.connect Ex: final prefs = await SharedPreferences.getInstance();
var ip = prefs.getString('ip');
await Meteor.connect('ws://$ip/websocket'); |
@wendellrocha i will try and test it tomorrow 😄 |
@wendellrocha i test it ready , for ip that i stored with shared_preferences it change , the problem is Meteor.connect() it don't get the ip i just changed , by the way i don't see any method how can i reconnect meteor again ( example : Meteor.reconnect(); ) |
You can disconnect with If you reconnect without disconnecting first, Meteor will probably open another connection. enhanced_meteorify/lib/src/meteor/meteor.dart Lines 149 to 157 in c514179
|
@wendellrocha i did what you mention it still not work , bro , here what i did on Provider
|
Did you try calling Meteor.connect() after disconnect? From what I noticed the reconnect will call the connect with the IP of the previous connection. |
@wendellrocha yeah, but i don't test with Meteor.connect() after disconnect yet , bro , let me test it |
@wendellrocha the right way to listen real time connect to meteor i want to know it connect or not :
|
@YouSour Using ConnectionListener is the fastest way to receive notifications from the connection. I have no experience with Provider, so I did a quick example in "pure" Flutter and the connection worked. I hope it helps you with something. Codeimport 'package:enhanced_meteorify/enhanced_meteorify.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() async {
try {
WidgetsFlutterBinding.ensureInitialized();
final ip = await PrefsService.getString('ip');
ConnectionService connection = ConnectionService();
if (ip.isNotEmpty) {
connection.setIP(ip);
final status = await connection.connect();
print(status);
}
runApp(MyApp());
} on MeteorError catch (error) {
print(error.message);
}
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _controller = TextEditingController();
void _connect() {
if (_controller.text.isNotEmpty) {
PrefsService.setString('ip', _controller.text);
ConnectionService conn = ConnectionService();
conn.setIP(_controller.text);
conn.connect();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(controller: _controller),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => _connect(),
tooltip: 'Cpnnect',
child: Icon(Icons.settings_ethernet),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
class PrefsService {
static void setString(String key, String item) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString(key, item);
}
static Future<String> getString(String key) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(key) ?? '';
}
}
class ConnectionService {
late String _ip;
void setIP(String ip) => this._ip = ip;
String get ip => this._ip;
bool get isConnected => Meteor.isConnected;
Future<ConnectionStatus> connect() async {
if (isConnected) disconnect();
return await Meteor.connect('ws://${this._ip}/websocket');
}
void disconnect() {
Meteor.disconnect();
}
}
|
@YouSour i ' ll check and work on it with your quick example tomorrow , it midnight here , i will confirm you , if i have a problem 🙂 |
@wendellrocha i test your example code , if i want to use Meteor.connectionListener where should i put that line ? because i need to check if connection is connected i show login page to user and if connection is disconnect i show connect page to user |
I can't think of a "right place" to use listener. You can put it somewhere in your project where you can listen for connection changes and make decisions about what to display to the user. |
@wendellrocha dynamic ip it works now , but Meteor.connectionListener look like it isn't listen when i stop meteor project and then i run meteor project again Meteor.connectionListener keep disconnected |
Meteor.connectionListener only notified that it was connected after login. Now it notifies you as soon as it successfully connects to Meteor. Can you test before publishing a new version? dependencies:
enhanced_meteorify:
git:
url: git://github.com:wendellrocha/enhanced_meteorify.git |
@wendellrocha i test it , Meteor.connectionListener it can listen after i stop meteor project and run again , but it connected and then it disconnect again |
here what it look like : https://drive.google.com/file/d/1IxMXZOZhtCLKXIoMlT9JRsW9AjjQAsBQ/view?usp=sharing |
The listener did the right thing. Noticed disconnection after DDP disconnected by an error. |
@wendellrocha sorry , i was a bit busy , i try to test it again and again and i also test your example code it get that error too , i noted every time i set a new ip address to Meteor.connect(); i got this error :
and then it disconnect , i think a new ip address that i just input it doesn't replace on Meteor.connect() , what is the main problem ? 🤔 |
No route to host. The device cannot find the server. Check the server IP. |
@wendellrocha did you test your example code that you give me ? it doesn't work too , is it work for you or not ? |
@wendellrocha can you test like this , first you put a wrong ip address then input the right one again , it work or not ? |
I forked the package because I needed to make some adjustments to the project I was working on. I'm no longer on this project. So, let's take it easy on the demands, I move this package when I have the time and patience. |
@wendellrocha it look like something wrong with |
Stale issue message |
@wendellrocha any news ? |
Stale issue message |
hi, do you have any Example App of make dynamic ip address for meteor connection , what i'm doing right now i have text field for user input their ip address and then i store it with shared_preferences so i get it later to use with
Meteor.connect();
and reconnect to meteor and i also need to listen to connectionawait Meteor.connect('ws://$_ipAddress/websocket');
The text was updated successfully, but these errors were encountered: