2016-07-06 01:28:00 +00:00
|
|
|
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;
|
2016-07-06 01:28:00 +00:00
|
|
|
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;
|
|
|
|
}
|