2016-12-23 10:47:21 +00:00
|
|
|
/// Command-line WebSocket client library for the Angel framework.
|
2016-12-23 20:57:46 +00:00
|
|
|
library angel_websocket.io;
|
2016-12-23 10:47:21 +00:00
|
|
|
|
|
|
|
import 'dart:async';
|
2016-12-23 20:57:46 +00:00
|
|
|
import 'dart:io';
|
2016-12-23 10:47:21 +00:00
|
|
|
import 'package:angel_client/angel_client.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
2018-11-08 21:53:52 +00:00
|
|
|
import 'package:http/io_client.dart' as http;
|
2016-12-23 10:47:21 +00:00
|
|
|
import 'package:web_socket_channel/web_socket_channel.dart';
|
|
|
|
import 'package:web_socket_channel/io.dart';
|
|
|
|
import 'base_websocket_client.dart';
|
|
|
|
export 'package:angel_client/angel_client.dart';
|
|
|
|
export 'angel_websocket.dart';
|
|
|
|
|
2021-02-21 02:47:23 +00:00
|
|
|
final RegExp _straySlashes = RegExp(r"(^/)|(/+$)");
|
2016-12-23 10:47:21 +00:00
|
|
|
|
|
|
|
/// Queries an Angel server via WebSockets.
|
|
|
|
class WebSockets extends BaseWebSocketClient {
|
2017-06-30 23:09:03 +00:00
|
|
|
final List<IoWebSocketsService> _services = [];
|
2017-03-02 03:54:58 +00:00
|
|
|
|
2019-02-03 19:34:00 +00:00
|
|
|
WebSockets(baseUrl,
|
|
|
|
{bool reconnectOnClose = true, Duration reconnectInterval})
|
2021-02-21 02:47:23 +00:00
|
|
|
: super(http.IOClient(), baseUrl,
|
2019-02-03 19:34:00 +00:00
|
|
|
reconnectOnClose: reconnectOnClose,
|
|
|
|
reconnectInterval: reconnectInterval);
|
2016-12-23 10:47:21 +00:00
|
|
|
|
2017-03-02 03:54:58 +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-03-02 03:54:58 +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();
|
|
|
|
}
|
|
|
|
|
2016-12-23 10:47:21 +00:00
|
|
|
@override
|
2016-12-23 20:57:46 +00:00
|
|
|
Future<WebSocketChannel> getConnectedWebSocket() async {
|
2019-01-06 02:41:46 +00:00
|
|
|
var socket = await WebSocket.connect(websocketUri.toString(),
|
2017-04-10 01:45:45 +00:00
|
|
|
headers: authToken?.isNotEmpty == true
|
|
|
|
? {'Authorization': 'Bearer $authToken'}
|
|
|
|
: {});
|
2021-02-21 02:47:23 +00:00
|
|
|
return IOWebSocketChannel(socket);
|
2016-12-23 10:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2018-10-21 08:15:51 +00:00
|
|
|
IoWebSocketsService<Id, Data> service<Id, Data>(String path,
|
|
|
|
{Type type, AngelDeserializer<Data> deserializer}) {
|
2021-02-21 02:47:23 +00:00
|
|
|
var uri = path.replaceAll(_straySlashes, '');
|
|
|
|
return IoWebSocketsService<Id, Data>(socket, this, uri, type);
|
2016-12-23 10:47:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 08:15:51 +00:00
|
|
|
class IoWebSocketsService<Id, Data> extends WebSocketsService<Id, Data> {
|
2016-12-23 10:47:21 +00:00
|
|
|
final Type type;
|
|
|
|
|
2018-10-02 15:32:06 +00:00
|
|
|
IoWebSocketsService(
|
|
|
|
WebSocketChannel socket, WebSockets app, String uri, this.type)
|
2016-12-23 10:47:21 +00:00
|
|
|
: super(socket, app, uri);
|
|
|
|
}
|