achilleasa / dart_amqp

Dart AMQP client implementing protocol version 0.9.1
MIT License
79 stars 40 forks source link

Client timeout handling #39

Open jeremy1265 opened 4 years ago

jeremy1265 commented 4 years ago

Hi,

Any example of handling the client timeout?? and also can we set the timeout length manually??

Thanks.

korbacsk commented 4 years ago

I use this code after send and create listener consumer:

` String amqpMessageResponse; StreamSubscription streamSubscriptionMessage; Duration timeout = new Duration(seconds: 10);

streamSubscriptionMessage = consumerForListen.listen(
  (AmqpMessage amqpMessage) {
    print('consumerForListen -> onMessage, amqpMessage: ' + amqpMessage.payloadAsString);
  },
);

print('start listen');
waitingForMessage = true;
int counter = 0;
int waitingMillisec = 0;
do {
  print('listen counter: ' + counter.toString());

  streamSubscriptionMessage.onData((amqpMessage) {
    try {
      print('streamSubscriptionMessage -> onData, amqpMessage: ' + amqpMessage.payloadAsString);
    } catch (e) {}

    if (amqpMessage.payloadAsString == 'Test') {
      amqpMessageResponse = amqpMessage.payloadAsString;
      waitingForMessage = false;
    }
  });

  if (waitingForMessage) {
    await Future.delayed(const Duration(milliseconds: 500));
    waitingMillisec += 500;
    if (waitingMillisec > timeout.inMilliseconds) {
      waitingForMessage = false;
    }
  }

  counter++;
} while (waitingForMessage);`

   try{
      await streamSubscriptionMessage.cancel();
    }
    catch(e){

    }