async def broadcastLobbyUpdates(server_id, lobby_name): lobby = get_lobby(server_id, lobby_name) if lobby is None: return ("No lobby here :(", 404) # Get a listener from the lobby def stream(): listener = lobby.listenForUpdates() # returns a queue.Queue while True: msg = listener.get() msg = json.dumps(msg) yield "data: {}\n\n".format(msg).encode('utf-8') response = Response(stream(), mimetype="text/event-stream") response.headers['Cache-Control'] = 'no-cache' response.headers['X-Accel-Buffering'] = 'no' response.timeout = None return response
async def media(media_file): res = request.args.get('res') if res: path = base64.b64decode(res.encode()).decode('ascii') return await send_file(path) item = self._file_store.get_by_id(media_file) if item is None: return '', int( HTTPStatus.NOT_FOUND) # temp cast fix for quart/h11 bug _path = item.get_path() if request.method == 'HEAD': if 'getcaptioninfo.sec' in request.headers: if item.get_captions(): response = Response( '', headers={'CaptionInfo.sec': item.get_captions()}, mimetype='text/html') return response response = Response( '<html><p>Captions srt file not found</p></html>', int(HTTPStatus.NOT_FOUND), mimetype='text/html') return response part, start, end = self.get_range(request.headers) mime_type = item.get_mime_type() # async def generate(chunk_size=2**16): # Default to 64k chunks...too much buffering w 10 sec fast forwarding # async def generate(chunk_size=2**19): # Default to 512k chunks...generates "RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one" see https://stackoverflow.com/questions/49093623/strange-behaviour-when-task-added-to-empty-loop-in-different-thread TODO: debug this # async def generate(chunk_size=2**18): # Default to 256k chunks ...same errors as 512 async def generate(chunk_size=2**17): # Default to 128k chunks async with aiofiles.open(_path, 'rb') as f: await f.seek(start) while True: data = await f.read(chunk_size) if not data: break yield data stats = os.stat(_path) end = stats.st_size if end is None else end size = str(end - start) headers = { 'Content-Length': size, 'Content-Type': mime_type, 'Accept-Ranges': 'bytes', # DLNA.ORG_OP = Time range capable / Byte range capable 'Contentfeatures.dlna.org': 'DLNA.ORG_OP=01' # TV will try to read entire file without this } if part: headers['Content-Range'] = f'bytes {start}-{end-1}/{size}' response = await make_response( (generate(), int(HTTPStatus.PARTIAL_CONTENT if part else HTTPStatus.OK), headers)) # Workaround for h11 string formatting bug # print('outbound headers', response.headers) response.timeout = None # No timeout return response