-
-
Save Hecatoncheir/01d94b122ee1a96f259aa8353f57afd4 to your computer and use it in GitHub Desktop.
mqtt subscribe test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
commented
Dec 13, 2017
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment