-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp.py
More file actions
65 lines (44 loc) · 1.81 KB
/
app.py
File metadata and controls
65 lines (44 loc) · 1.81 KB
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
from flask import Flask, request, send_from_directory
from flask_socketio import SocketIO, emit
from dotenv import load_dotenv
from os import environ as env
load_dotenv()
app = Flask(__name__, static_url_path="", static_folder="./build")
app.config["SECRET_KEY"] = env["SECRET_KEY"]
socketio = SocketIO(app) # , async_mode='gevent')
owners = {}
user_sid = {}
tx_hash = {}
@socketio.on("owner connected", namespace="/websocket")
def handle_owner_connection(address):
owners[address] = request.sid
if not address in list(tx_hash.keys()):
tx_hash[address] = {"previous": None, "current": None}
@socketio.on("user request", namespace="/websocket")
def handle_user_request(json_data):
req_data = json_data["data"]
address = json_data["address"]
user_sid[address] = request.sid
tx_hash[address]["current"] = json_data["tx_hash"]
emit("request status", "server-received", room=user_sid[address])
owner_session_id = owners[address]
emit("user request", req_data, room=owner_session_id)
@socketio.on("request status", namespace="/websocket")
def handle_request_status(json_data):
status = json_data["status"]
address = json_data["address"]
if status == "owner-data processed":
tx_hash[address]["previous"] = tx_hash[address]["current"]
tx_hash[address]["current"] = None
emit("request status", status, room=user_sid[address])
@socketio.on("transaction hash", namespace="/websocket")
def handle_owner_connection(address):
return tx_hash[address]["current"]
@app.route("/", defaults={"path": ""})
def serve(path):
return send_from_directory(app.static_folder, "index.html")
@app.route("/<path>")
def catch_all(path):
return send_from_directory(app.static_folder, "index.html")
if __name__ == "__main__":
socketio.run(app, host="0.0.0.0", port=5000)