Skip to content

Instantly share code, notes, and snippets.

@Hecatoncheir
Last active January 15, 2018 13:11
Show Gist options
  • Select an option

  • Save Hecatoncheir/01d94b122ee1a96f259aa8353f57afd4 to your computer and use it in GitHub Desktop.

Select an option

Save Hecatoncheir/01d94b122ee1a96f259aa8353f57afd4 to your computer and use it in GitHub Desktop.
mqtt subscribe test
test('can subscribe and send events to mq server', () async {
MqttConnectionIOWebSocket connection1 =
new MqttConnectionIOWebSocket.setOptions(host: mqIp);
MqttClient<MqttConnectionIOWebSocket> client1 =
new MqttClient<MqttConnectionIOWebSocket>(connection1,
clientID: 'FirstClient',
qos: QOS_1,
userName: login,
userPassword: password);
client1.debugMessage = true;
await client1.connect(null, login, password);
StreamController<String> controller = new StreamController<String>();
Stream<String> stream = controller.stream.asBroadcastStream();
client1.subscribe('server/#', QOS_1, (String router, String message) {
controller.add(message);
});
MqttConnectionIOWebSocket connection2 =
new MqttConnectionIOWebSocket.setOptions(host: mqIp);
MqttClient<MqttConnectionIOWebSocket> client2 =
new MqttClient<MqttConnectionIOWebSocket>(connection2,
clientID: 'SecondClient',
qos: QOS_1,
userName: login,
userPassword: password);
client2.debugMessage = true;
await client2.connect(null, login, password);
Map<String, dynamic> sendedEntity = <String, dynamic>{
'id': 1,
'lastChangeTime': '2017-07-19T10:18:38.444135Z',
'isActive': true
};
await client2.publish(
'server.log.web.test.a8fe5020-d2d0-11e7-d08b-6d9310df6f6b',
JSON.encode(sendedEntity));
await for (String message in stream) {
String expectedEntity =
'{\"id\":1,\"lastChangeTime\":\"2017-07-19T10:18:38.444135Z\"}';
expect(message, equals(expectedEntity));
break;
}
client1.disconnect();
client2.disconnect();
controller.close();
});
@Hecatoncheir

Copy link
Copy Markdown
Author
00:00 +0: loading test/server/mqtt_test.dart00:00 +0: Server can subscribe and send events to mq server00:00 +0: Server can subscribe and send events to mq server
Opening session
>>> Type(1) Len(48) QoS(1) DUP(0) retain(0) <184606778173115100112319403001170105114115116671081051011101160811410911397100109105110091221144911811453102102105>]
<<< Type(2) Len(4) QoS(0) DUP(0) retain(0) <>]
Connection Accepted
Subscribe to server/# - QoS: 1 - Message ID: 1
>>> Type(8) Len(15) QoS(1) DUP(0) retain(0) <13013010811510111411810111447351>]
Opening session
>>> Type(1) Len(49) QoS(1) DUP(0) retain(0) <18470677817311510011231940300128310199111110100671081051011101160811410911397100109105110091221144911811453102102105>]
[Suback] Granted QOS level: 1
<<< Type(9) Len(5) QoS(0) DUP(0) retain(0) <>]
<<< Type(2) Len(4) QoS(0) DUP(0) retain(0) <>]
Connection Accepted
Publish server/log/web/test.a8fe5020-d2d0-11e7-d08b-6d9310df6f6b: {"id":1,"lastChangeTime":"2017-07-19T10:18:38.444135Z"} (ID:1 - QoS: 0 - retain: 0)
>>> Type(3) Len(164) QoS(0) DUP(0) retain(0) <48162105611510111411810111447108111103471191019847116101115116469756102101534850484510050100484549491015545100485698455410057514948100102541025498123341051003458494434108971151166710497110103101841051091013458345048495545485545495784494858495658515646525252495153903444341001111099710511073100345849443410011110997105110789710910134583411610111511634443410511565991161051181013458116114117101125>]
00:00 +0 -1: Server can subscribe and send events to mq server [E]
  RangeError (end): Invalid value: Not in range 2..163, inclusive: 258
  dart:typed_data                                    _TypedListBase.sublist
  package:mqtt/mqtt_message_publish.dart 90:31       MqttMessagePublish.decodeVariableHeader
  package:mqtt/mqtt_message.dart 45:17               new MqttMessage.decode
  package:mqtt/mqtt_message_publish.dart 29:140      new MqttMessagePublish.decode
  package:mqtt/mqtt_client.dart 316:34               MqttClient._handlePublish
  package:mqtt/mqtt_client.dart 218:30               MqttClient._processMqttMessage
  package:mqtt/mqtt_client.dart 193:16               MqttClient._processData
  package:mqtt/mqtt_connection_io_socket.dart 32:29  MqttConnectionIOSocket.startListening.<fn>
  ===== asynchronous gap ===========================
  dart:io                                            _Socket.listen
  package:mqtt/mqtt_connection_io_socket.dart 31:13  MqttConnectionIOSocket.startListening
  package:mqtt/mqtt_client.dart 162:21               MqttClient._handleConnected
  package:mqtt/mqtt_client.dart 54:49                MqttClient.connect.<fn>
  ===== asynchronous gap ===========================
  dart:async                                         _Future.then
  package:mqtt/mqtt_client.dart 54:31                MqttClient.connect
  test/server/mqtt_test.dart 34:21                   main.<fn>.<fn>
  ===== asynchronous gap ===========================
  dart:async                                         new Future.microtask
  test/server/mqtt_test.dart 34:65                   main.<fn>.<fn>

00:00 +0 -1: Some tests failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment