-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
executable file
·104 lines (77 loc) · 2.46 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
from gevent import monkey
monkey.patch_all()
import os
from pathlib import Path
from typing import MutableMapping
from typing import Optional
from flask import Flask
from flask import Request
from flask import request
from flask_socketio import emit
from typing_extensions import TypedDict
from asynction import AsynctionSocketIO
Sid = str
Username = str
username_store: MutableMapping[Sid, Username] = {}
def get_sid(request: Request) -> Sid:
return request.sid # type: ignore
def new_message(message: str) -> None:
sid = get_sid(request)
username = username_store[sid]
emit(
"new message",
{"username": username, "message": message},
broadcast=True,
skip_sid=sid,
)
def message_typing() -> None:
sid = get_sid(request)
username = username_store[sid]
emit("typing", {"username": username}, broadcast=True, skip_sid=sid)
def stop_typing() -> None:
sid = get_sid(request)
username = username_store[sid]
emit("stop typing", {"username": username}, broadcast=True, skip_sid=sid)
class AddUserAck(TypedDict):
error: Optional[str]
def add_user(username: str) -> AddUserAck:
if username in username_store.values():
return AddUserAck(error=f"Username {username} already exists")
sid = get_sid(request)
username_store[sid] = username
num_users = len(username_store)
emit("login", {"numUsers": num_users})
emit(
"user joined",
{"username": username, "numUsers": num_users},
broadcast=True,
skip_sid=sid,
)
return AddUserAck(error=None)
def disconnect() -> None:
sid = get_sid(request)
username = username_store.pop(sid, None)
if username is not None:
emit(
"user left",
{"username": username, "numUsers": len(username_store)},
broadcast=True,
skip_sid=sid,
)
def admin_connect() -> None:
token = request.args.get("token")
if token != "admin":
raise ConnectionRefusedError("Unauthorized!")
emit("server metric", {"name": "CPU_COUNT", "value": os.cpu_count()})
flask_app = Flask(__name__)
asio = AsynctionSocketIO.from_spec(
spec_path=Path(__file__).parent / "asyncapi.yml",
server_name=os.environ.get("ASYNCAPI_SERVER_NAME", "demo"),
logger=True,
async_mode="gevent",
app=flask_app,
cors_allowed_origins="*",
)
if __name__ == "__main__":
asio.run(app=flask_app, debug=True, log_output=True, host="0.0.0.0")