platform/lib/websocket_context.dart

35 lines
1.1 KiB
Dart
Raw Normal View History

part of angel_websocket.server;
class WebSocketContext {
2016-09-18 01:35:16 +00:00
StreamController<Map> _onAll = new StreamController<Map>.broadcast();
StreamController _onData = new StreamController.broadcast();
_WebSocketEventTable on = new _WebSocketEventTable();
Stream<Map> get onAll => _onAll.stream;
Stream get onData => _onData.stream;
WebSocket underlyingSocket;
RequestContext requestContext;
ResponseContext responseContext;
WebSocketContext(WebSocket this.underlyingSocket,
RequestContext this.requestContext, ResponseContext this.responseContext);
send(String eventName, data) {
underlyingSocket.add(
god.serialize(new WebSocketEvent(eventName: eventName, data: data)));
}
2016-12-23 10:47:21 +00:00
sendError(AngelHttpException error) => send("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))
_handlers[eventName] = new StreamController<Map>.broadcast();
return _handlers[eventName];
}
Stream<Map> operator [](String key) => _getStreamForEvent(key).stream;
}