connect method
Implementation
Future<void> connect() async {
final completer = Completer<void>();
await _tcpClient.connect(options.host, options.port, options.timeout);
_tcpStream = _tcpClient.onReceive;
_subscription = _tcpStream!.listen(
(data) {
final frame = ServerFrame.parse(String.fromCharCodes(data));
if (frame.command == ServerCommand.CONNECTED) {
if (!completer.isCompleted) {
completer.complete();
}
}
if (frame.command == ServerCommand.MESSAGE) {
final destination = frame.headers[StompHeaders.destination];
final handler = _handlers[destination!];
if (handler != null) {
handler(StompMessage(frame.headers, frame.body!));
}
}
if (frame.command == ServerCommand.ERROR) {
_tcpClient.close();
if (!completer.isCompleted) {
completer.completeError(StompException(frame.body!));
}
}
},
onDone: () {
_tcpClient.close();
if (!completer.isCompleted) {
completer.completeError(StompException('Connection closed'));
}
},
onError: (error) {
print('Error: $error');
_tcpClient.close();
if (!completer.isCompleted) {
completer.completeError(StompException('Connection error'));
}
},
);
_tcpClient.add(ClientFrame.connect(
options.host,
options.login,
options.passcode,
).toBytes());
await completer.future;
}