messenger v0.9
This commit is contained in:
parent
5b54f3f636
commit
ae6bf0f96d
13 changed files with 105 additions and 115 deletions
39
server.py
39
server.py
|
|
@ -1,27 +1,60 @@
|
|||
# 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(connection, path):
|
||||
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.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(launch_session, HOST, PORT)
|
||||
await websockets.serve(main, HOST, PORT)
|
||||
Loading…
Reference in a new issue