2016-07-06 01:28:00 +00:00
|
|
|
part of angel_websocket.server;
|
|
|
|
|
2016-12-23 20:57:46 +00:00
|
|
|
/// Represents a WebSocket session, with the original
|
|
|
|
/// [RequestContext] and [ResponseContext] attached.
|
2016-07-06 01:28:00 +00:00
|
|
|
class WebSocketContext {
|
2016-12-23 20:57:46 +00:00
|
|
|
/// Use this to listen for events.
|
2016-09-18 01:35:16 +00:00
|
|
|
_WebSocketEventTable on = new _WebSocketEventTable();
|
2016-12-23 20:57:46 +00:00
|
|
|
|
|
|
|
/// The underlying [WebSocket] instance.
|
|
|
|
final WebSocket io;
|
|
|
|
|
|
|
|
/// The original [RequestContext].
|
|
|
|
final RequestContext request;
|
|
|
|
|
|
|
|
/// The original [ResponseContext].
|
|
|
|
final ResponseContext response;
|
|
|
|
|
|
|
|
StreamController<WebSocketAction> _onAction =
|
|
|
|
new StreamController<WebSocketAction>();
|
2017-02-28 14:15:34 +00:00
|
|
|
|
|
|
|
StreamController<Null> _onClose = new StreamController<Null>();
|
|
|
|
|
2016-12-23 20:57:46 +00:00
|
|
|
StreamController _onData = new StreamController();
|
|
|
|
|
|
|
|
/// Fired on any [WebSocketAction];
|
|
|
|
Stream<WebSocketAction> get onAction => _onAction.stream;
|
|
|
|
|
2017-02-28 14:15:34 +00:00
|
|
|
/// Fired once the underlying [WebSocket] closes.
|
|
|
|
Stream<Null> get onClose => _onClose.stream;
|
|
|
|
|
2016-12-23 20:57:46 +00:00
|
|
|
/// Fired when any data is sent through [io].
|
2016-09-18 01:35:16 +00:00
|
|
|
Stream get onData => _onData.stream;
|
2016-07-06 01:28:00 +00:00
|
|
|
|
2016-12-23 20:57:46 +00:00
|
|
|
WebSocketContext(WebSocket this.io, RequestContext this.request,
|
|
|
|
ResponseContext this.response);
|
2016-07-06 01:28:00 +00:00
|
|
|
|
2017-02-28 14:15:34 +00:00
|
|
|
/// Closes the underlying [WebSocket].
|
|
|
|
Future close([int code, String reason]) async {
|
|
|
|
await io.close(code, reason);
|
|
|
|
_onAction.close();
|
|
|
|
_onData.close();
|
|
|
|
_onClose.add(null);
|
|
|
|
_onClose.close();
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:57:46 +00:00
|
|
|
/// Sends an arbitrary [WebSocketEvent];
|
|
|
|
void send(String eventName, data) {
|
|
|
|
io.add(god.serialize(new WebSocketEvent(eventName: eventName, data: data)));
|
2016-07-06 01:28:00 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 20:57:46 +00:00
|
|
|
/// Sends an error event.
|
|
|
|
void sendError(AngelHttpException error) => send(EVENT_ERROR, error.toJson());
|
2016-09-03 12:34:01 +00:00
|
|
|
}
|
2016-09-18 01:35:16 +00:00
|
|
|
|
|
|
|
class _WebSocketEventTable {
|
|
|
|
Map<String, StreamController<Map>> _handlers = {};
|
|
|
|
|
|
|
|
StreamController<Map> _getStreamForEvent(eventName) {
|
|
|
|
if (!_handlers.containsKey(eventName))
|
2017-02-28 14:15:34 +00:00
|
|
|
_handlers[eventName] = new StreamController<Map>();
|
2016-09-18 01:35:16 +00:00
|
|
|
return _handlers[eventName];
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream<Map> operator [](String key) => _getStreamForEvent(key).stream;
|
|
|
|
}
|