# server.py import json import uuid import asyncio import websockets import typs import session HOST = "localhost" PORT = 8764 token_refs = {} # Makes instance of a Messenger session for every client that connects. async def launch_session(token, connection): # Create the session. s = session.Session(connection) # Login if token. if token in token_refs.keys(): crimata_id = token_refs.get(token) s.crimata_id = crimata_id print(f"Reconnected: {crimata_id}") # Update client's login token. intent = typs.Intent("token", params={"text": token}) await s.send_agent_intent(intent) # Run core loops. await asyncio.gather( s.do_agent_intents(), s.deliver_mail() ) # Update state before end. token_refs.update({token: s.crimata_id}) print("Ended session.") async def main(connection, path): token = await connection.recv() # Create Session ID if None. if token not in token_refs.keys(): token = str(uuid.uuid4()) # Start session with token. await launch_session(token, connection) #! Not stopping on disconnect # Run run for every new connection. async def lift(): print(f"Listening for connections on {HOST}:{PORT}") await websockets.serve(main, HOST, PORT)