Created
December 6, 2023 09:34
-
-
Save hansen1416/ce1f39bd9af51f926f283d9b93004275 to your computer and use it in GitHub Desktop.
stomp-dart-client-demo
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
// dependencies: | |
// stomp_dart_client: ^1.0.0 | |
import 'dart:async'; | |
import 'dart:convert'; | |
import 'package:stomp_dart_client/stomp.dart'; | |
import 'package:stomp_dart_client/stomp_config.dart'; | |
import 'package:stomp_dart_client/stomp_frame.dart'; | |
void onConnect(StompFrame frame) { | |
print('Connected, start subscription to /topic/device/126/metric'); | |
stompClient.subscribe( | |
destination: '/topic/device/126/metric', | |
callback: (frame) { | |
print('received message'); | |
Map<String, dynamic>? result = json.decode(frame.body!); | |
print(result); | |
}, | |
); | |
} | |
final stompClient = StompClient( | |
config: StompConfig( | |
url: 'wss://*******/websocket/ws', | |
onConnect: onConnect, | |
beforeConnect: () async { | |
print('waiting to connect...'); | |
await Future.delayed(const Duration(milliseconds: 200)); | |
print('connecting...'); | |
}, | |
onWebSocketError: (dynamic error) => print(error.toString()), | |
// stompConnectHeaders: {'Authorization': 'Bearer yourToken'}, | |
webSocketConnectHeaders: {'Cookie': '*******'}, | |
), | |
); | |
void main() { | |
stompClient.activate(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment