platform/packages/websocket/lib/flutter.dart

50 lines
1.5 KiB
Dart
Raw Normal View History

2017-06-30 23:09:03 +00:00
/// Flutter-compatible WebSocket client library for the Angel framework.
library angel_websocket.flutter;
import 'dart:async';
import 'dart:io';
2018-11-08 21:53:52 +00:00
import 'package:http/io_client.dart' as http;
2017-06-30 23:09:03 +00:00
import 'package:web_socket_channel/web_socket_channel.dart';
import 'package:web_socket_channel/io.dart';
import 'base_websocket_client.dart';
2021-05-15 07:19:35 +00:00
export 'package:angel3_client/angel3_client.dart';
export 'angel3_websocket.dart';
2017-06-30 23:09:03 +00:00
2021-05-15 07:19:35 +00:00
// final RegExp _straySlashes = RegExp(r"(^/)|(/+$)");
2017-06-30 23:09:03 +00:00
/// Queries an Angel server via WebSockets.
class WebSockets extends BaseWebSocketClient {
final List<WebSocketsService> _services = [];
WebSockets(baseUrl,
2021-04-26 00:47:32 +00:00
{bool reconnectOnClose = true, Duration? reconnectInterval})
2021-02-21 02:47:23 +00:00
: super(http.IOClient(), baseUrl,
reconnectOnClose: reconnectOnClose,
reconnectInterval: reconnectInterval);
2017-06-30 23:09:03 +00:00
@override
2019-01-06 02:41:46 +00:00
Stream<String> authenticateViaPopup(String url,
{String eventName = 'token'}) {
2021-02-21 02:47:23 +00:00
throw UnimplementedError(
2017-06-30 23:09:03 +00:00
'Opening popup windows is not supported in the `dart:io` client.');
}
@override
Future close() {
for (var service in _services) {
service.close();
}
return super.close();
}
@override
Future<WebSocketChannel> getConnectedWebSocket() async {
2019-01-06 02:41:46 +00:00
var socket = await WebSocket.connect(websocketUri.toString(),
2017-06-30 23:09:03 +00:00
headers: authToken?.isNotEmpty == true
? {'Authorization': 'Bearer $authToken'}
: {});
2021-02-21 02:47:23 +00:00
return IOWebSocketChannel(socket);
2017-06-30 23:09:03 +00:00
}
2018-10-02 15:32:06 +00:00
}