part of angel_websocket.server; class WebSocketContext { StreamController _onAll = new StreamController.broadcast(); StreamController _onData = new StreamController.broadcast(); _WebSocketEventTable on = new _WebSocketEventTable(); Stream 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))); } sendError(AngelHttpException error) => send("error", error.toJson()); } class _WebSocketEventTable { Map> _handlers = {}; StreamController _getStreamForEvent(eventName) { if (!_handlers.containsKey(eventName)) _handlers[eventName] = new StreamController.broadcast(); return _handlers[eventName]; } Stream operator [](String key) => _getStreamForEvent(key).stream; }