shamblett / coap

A Coap package for dart
Other
16 stars 13 forks source link

Ping to two different hosts doesn't work? #29

Closed kwekel closed 2 years ago

kwekel commented 3 years ago

Hi, I'm trying to ping different servers using the CoAP package in my Flutter app. The first one works and returns true. The second however gives back an error. In this example I've used the same host, but in the real app I would of course use a different host. The reason I would like to ping different hosts is because I want to discover hardware on my local network by pinging different hosts.

  pingCoap() async {
    var conf = CoapConfig();

    Uri uri = Uri(
      scheme: 'coap', 
      host: 'coap.me',
      port: conf.defaultPort,
    );

    var client1 = CoapClient(uri, conf);
    bool firstPingResponse = await client1.ping(10000);
    client1.close();
    // This works
    print(firstPingResponse);

    Uri uri1 = Uri(
      scheme: 'coap', 
      host: 'coap.me',
      port: conf.defaultPort,
    );

    var client2 = CoapClient(uri1, conf);
    bool secondPingResponse = await client2.ping(10000);
    client2.close();

    print(secondPingResponse);
  }

This is the error in the console:

[VERBOSE-2:ui_dart_state.cc(199)] Unhandled Exception: RangeError: Index out of range: no indices are valid: 0

0 TypedDataBuffer.[] (package:typed_data/src/typed_buffer.dart:32:26)

1 ListMixin.removeAt (dart:collection/list.dart:538:20)

2 CoapDatagramReader._readCurrentByte (package:coap/src/codec/datagram/coap_datagram_reader.dart:81:26)

3 CoapDatagramReader.read (package:coap/src/codec/datagram/coap_datagram_reader.dart:34:9)

4 CoapMessageDecoder18.readProtocol (package:coap/src/codec/decoders/coap_message_decoder18.dart:23:25)

5 new CoapMessageDecoder18 (package:coap/src/codec/decoders/coap_message_decoder18.dart:14:5)

6 CoapDraft18.newMessageDecoder (package:coap/src/specification/drafts/coap_draft18.dart:52:7)

7 CoapEndPoint._receiveData (package:coap/src/net/coap_endpoint.dart:96:35)

8 _rootRunUnary (dart:async/zone.dart:1362:47)

9 _CustomZone.runUnary (dart:async/zone.dart:1265:19)

10 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)

11 CastStreamSubscription._onData (dart:_internal/async_cast.dart:85:11)

12 _rootRunUnary (dart:async/zone.dart:1362:47)

13 _CustomZone.runUnary (dart:async/zone.dart:1265:19)

14 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)

15 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)

16 _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)

17 _ForwardingStreamSubscription._add (dart:async/stream_pipe.dart:123:11)

18 _WhereStream._handleData (dart:async/stream_pipe.dart:195:12)

19 _ForwardingStreamSubscription._handleData (dart:async/stream_pipe.dart:153:13)

20 _rootRunUnary (dart:async/zone.dart:1362:47)

21 _CustomZone.runUnary (dart:async/zone.dart:1265:19)

22 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)

23 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)

24 _DelayedData.perform (dart:async/stream_impl.dart:591:14)

25 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)

26 _PendingEvents.schedule. (dart:async/stream_impl.dart:663:7)

27 _rootRun (dart:async/zone.dart:1346:47)

28 _CustomZone.run (dart:async/zone.dart:1258:19)

29 _CustomZone.runGuarded (dart:async/zone.dart:1162:7)

30 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1202:23)

31 _rootRun (dart:async/zone.dart:1354:13)

32 _CustomZone.run (dart:async/zone.dart:1258:19)

33 _CustomZone.runGuarded (dart:async/zone.dart:1162:7)

34 _CustomZone.bindCallbackGuarded. (dart:async/zone.dart:1202:23)

35 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)

36 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)

Am I doing something wrong? Or is it not possible to ping different hosts?

shamblett commented 3 years ago

I can't see any reason why this wouldn't work, unless its because its the same host but even so, I'll have a look at this.

a1573595 commented 3 years ago

I can't see any reason why this wouldn't work, unless its because its the same host but even so, I'll have a look at this.

Eventbus is the main reason, I can’t find a way to reset it, only destroy method, and it is Singleton, so I used stream instead of it.

shamblett commented 3 years ago

Do you mean you got rid of event bus entirely and used your own solution?

a1573595 commented 3 years ago

When you have multiple clients, EventBus can’t knowe which one to deliver, it will notify all, and then I can't find a way to unsubscribe EventBus when Client closed, so I use Stream for CoapDataReceivedEvent & CoapCompletedEvent, it is work.

shamblett commented 3 years ago

Yes your right, I thought I'd addressed this a while ago, clearly not, I'll look at it again, thanks.

Sorunome commented 2 years ago

Ironically, #40 should also fix this