Beispiel #1
0
async def handle_websocket(request, path):
    """
    Handle web socket requests and return the poll information

    Check every 0.2 seconds to get the latest position and send if it changed. This only gets triggered when the first
    client connects.

    :param request: request from client
    :param path: determines the endpoints supported
    """
    log.debug('WebSocket handler registered with client')
    previous_poll = None
    previous_main_poll = None
    poller = Registry().get('poller')
    if path == '/state':
        while True:
            current_poll = poller.poll()
            if current_poll != previous_poll:
                await request.send(json.dumps(current_poll).encode())
                previous_poll = current_poll
            await asyncio.sleep(0.2)
    elif path == '/live_changed':
        while True:
            main_poll = poller.main_poll()
            if main_poll != previous_main_poll:
                await request.send(main_poll)
                previous_main_poll = main_poll
            await asyncio.sleep(0.2)