Closed thejoker101 closed 3 years ago
I think it might be this bit here in coap_event_bus.dart:
void fire(dynamic event) {
if (!_destroyed) {
lastEvent = event;
_eventBus.fire(event);
} else {
_log.warn('Event Bus - attempting to raise event on '
'destroyed event bus : $event');
}
}
It looks like _destroyed
is never set to true in destroy()
Changing _destroy
to be bool _destroyed = false;
and modifying destroy()
to look like this
/// Destroy
void destroy() {
_eventBus.destroy();
_destroyed = true;
}
does fix the exception, however things are still not correct it seems. These warnings are still in the logs:
[ ] I/flutter ( 9248): 2020-10-15 15:23:39.436: INFO: >> Reliability - sending request, failed transmission count: 0
[ ] I/flutter ( 9248): 2020-10-15 15:23:39.436: INFO: >> Reliability - Retransmission timeout is 3444 ms
[ ] I/flutter ( 9248): 2020-10-15 15:23:39.437: INFO: >> Matcher - Stored open request by KeyID[32090]) + KeyToken[00001634]
[ ] I/flutter ( 9248): 2020-10-15 15:23:39.438: WARNING: >> Event Bus - attempting to raise event on destroyed event bus : Instance of 'CoapSendingRequestEvent'
[ +199 ms] I/flutter ( 9248): 2020-10-15 15:23:39.637: WARNING: >> Event Bus - attempting to raise event on destroyed event bus : Instance of 'CoapDataReceivedEvent'
[+3242 ms] I/flutter ( 9248): 2020-10-15 15:23:42.882: WARNING: >> Reliability - Retransmission timeout elapsed
[ ] I/flutter ( 9248): 2020-10-15 15:23:42.882: WARNING: >> Reliability - Timeout: retransmit message, failed count: 1 message: 32090
My guess is the issue is because the _eventBus
is a static singleton that's being reused.
OK, the flutter example didn't come from me, it was added by user @pedromassango on #5 I'm not a flutter user so have no idea if this still works or has ever worked for that matter, may be better to contact him directly on how this is supposed to work.
You right in the fact that the event bus can't be re-used, the package is not intended for multiple instantiations at the moment, in my other packages the event bus is instance specific, I've not got round to updating this [package yet.
OK, the flutter example didn't come from me, it was added by user @pedromassango on #5 I'm not a flutter user so have no idea if this still works or has ever worked for that matter, may be better to contact him directly on how this is supposed to work.
You right in the fact that the event bus can't be re-used, the package is not intended for multiple instantiations at the moment, in my other packages the event bus is instance specific, I've not got round to updating this [package yet.
Thanks, I was thinking the issue was more with the example flutter code. I'll close this issue.
FWIW, if anyone else comes across this page and has this issue. I modified the Flutter example as below and now you can click the load button all you want without Exceptions.
import 'package:coap/coap.dart';
import 'package:flutter/material.dart';
import 'config/coap_config.dart';
void main() => runApp(MyApp());
/// make linter happy
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
/// make linter happy
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _data;
bool _isLoading = false;
final _config = CoapConfig();
final host = 'coap.me';
// Client
CoapClient _client;
@override
void didChangeDependencies() {
super.didChangeDependencies();
_client = CoapClient(
Uri(scheme: 'coap', host: _host, port: _config.defaultPort), _config);
}
Future<void> _loadData() async {
try {
// Create the request for the get request
final CoapRequest request = CoapRequest.newGet();
request.addUriPath('hello');
_client.request = request;
setState(() => _isLoading = true);
final CoapResponse response = await _client.get();
setState(() {
_isLoading = false;
_data = response?.payloadString ?? 'No Data';
});
} catch (e) {
log.e("Caught Error Doing COAP Stuff", e);
}
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Flutter CoAP'),
),
body: Center(
child: Builder(
builder: (_) {
if (_isLoading) {
return const CircularProgressIndicator();
}
if (_data != null) {
return Text('Response:\n $_data',
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 18)
);
}
return const Text('Press the button to load data');
},
),
),
floatingActionButton: FloatingActionButton(
onPressed: _loadData,
tooltip: 'Load',
child: Icon(Icons.cloud_download),
),
);
}
Hi @thejoker101 so the issue is not related to the code sample right?
Hi @thejoker101 so the issue is not related to the code sample right?
The example here is what I changed with the code above.
The main difference is moving the _client
instantiation out of _loadData
and not calling client.close();
at the end of _loadData
because once it's closed, it can't be re-used.
Hi @thejoker101 so the issue is not related to the code sample right?
The example here is what I changed with the code above.
The main difference is moving the
_client
instantiation out of_loadData
and not callingclient.close();
at the end of_loadData
because once it's closed, it can't be re-used.
I have tried this for put request. But after, it works for sometime. But after sometime, after about 20-25 requests, the app does not send commands, also the coap server gets hang. Please guide.
I'm trying to use this package in a flutter app. I'm using the example here - coap/example/flutter/lib/main.dart. When I press the Floating Action Button, it connects to my simple node.js server and displays the response text. However, if I press that button again, I get the exception below. Actually, it appears to be in some sort of Timer, so it just repeats the same error over and over again.