예제 #1
0
async def client_sock(sock: WebSocket):
    await sock.accept()
    client_id = generate_id()
    async with client_sock_lock:
        clients[client_id] = sock
    
    try:
        while True:
            try:
                data = await sock.receive_json()
                event = data["event"]

                if event == "CLOSE":
                    await sock.send_json({"event": "CLOSE"})
                    sock.close()
                    break
                elif event == "CONNECT":
                    await sock.send_json({"event": "CONNECT"})
                elif event == "UPD_SLIDER":
                    print(f"NEW VAL FOR SLIDER <{data['name']}>: {data['value']}")
                elif event == "SONG":
                    async with now_playing_sock_lock:
                        for i in socks.values():
                            await i.send_json(data["song"])

            except json.JSONDecodeError:
                await sock.send_json({"event": "F**k you"})
                continue
    except starlette.websockets.WebSocketDisconnect:
        print("Socket Disconnected")
    finally:
        async with client_sock_lock:
            clients.pop(client_id)
예제 #2
0
파일: main.py 프로젝트: cakir-enes/sechat
async def message_updates(ws: WebSocket, username: str):
    await ws.accept()
    challenge = pyotp.random_base32()
    await ws.send_json({"type": "CHALLENGE", "payload": challenge})
    # TODO CHECK THIS SHIT
    # resp = await ws.receive_text()
    # print(f"RESP: {resp}")
    async with lock:
        sessions[username] = ws
        print(sessions.keys())
    # valid = verify_signature(username, challenge, resp)
    valid = True
    if not valid:
        ws.send_text("thats not cool bro")
        ws.close()
        return

    while True:
        data = await ws.receive_text()