-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_io.py
113 lines (89 loc) · 3.29 KB
/
web_io.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
105
106
107
108
109
110
111
112
113
import os
import socket
import sys
import ssl
import eventlet
from eventlet import wsgi
import logging
from flask import Flask, request # Import 'request' here
from flask_socketio import SocketIO
from flask_uploads import UploadSet, configure_uploads
from pydispatch import dispatcher
# Patch system modules to be greenthread-friendly
eventlet.monkey_patch(socket=True, time=True, select=True, os=True)
# Another monkey patch to avoid annoying (and useless?) socket pipe warnings when users disconnect
import socketserver
from wsgiref import handlers
socketserver.BaseServer.handle_error = lambda *args, **kwargs: None
handlers.BaseHandler.log_exception = lambda *args, **kwargs: None
# Turn off more annoying log messages that aren't helpful.
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
app = Flask(__name__, static_folder='webpage')
app.config['SECRET_KEY'] = 'Big Whoop is an amusement park... or is it?!'
app.config['UPLOADED_JSON_DEST'] = '/tmp/'
socketio = SocketIO(app, async_mode='eventlet', ping_timeout=30, logger=False, engineio_logger=False)
# Configure server to accept uploads of JSON files
docs = UploadSet('json', ('json'))
configure_uploads(app, docs)
class WebServer:
@app.route("/")
def index():
return app.send_static_file('index.html')
def broadcast(self, id, data):
with app.app_context():
try:
socketio.emit(id, data, broadcast=True)
except Exception as e:
print(f"Broadcast error: {e}")
@app.route('/<path:path>')
def static_proxy(path):
return app.send_static_file(path)
@socketio.on('onConnect')
def connectEvent(msg):
# Retrieve the client's IP address
ip = request.remote_addr
dispatcher.send(signal='connectEvent', client_ip=ip)
@socketio.on('showPlay')
def showPlayEvent(show_name):
dispatcher.send(signal='showPlay', showName=show_name)
@socketio.on('showStop')
def showStopEvent():
dispatcher.send(signal='showStop')
@socketio.on('showPause')
def showPauseEvent():
dispatcher.send(signal='showPause')
@socketio.on('onKeyPress')
def webKeyEvent(data):
dispatcher.send(signal="keyEvent", key=data["keyVal"], val=int(data["val"]))
return data["keyVal"]
def __init__(self):
self.server = eventlet.spawn(self.run_server)
def run_server(self):
try:
# Start HTTP server on port 80 in a new green thread
print("Starting HTTP server on port 80...")
eventlet.spawn_n(socketio.run, app, host='0.0.0.0', port=80)
# HTTPS setup
print("Starting HTTPS server on port 443...")
# Create an SSL context
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ssl_context.load_cert_chain(certfile='server.crt', keyfile='server.key')
# Create a new SSL socket for HTTPS
https_socket = eventlet.wrap_ssl(eventlet.listen(('0.0.0.0', 443)),
certfile='server.crt',
keyfile='server.key',
server_side=True)
# Start HTTPS server using the SSL socket
eventlet.spawn_n(wsgi.server, https_socket, app)
# Keep the main thread alive to ensure the servers keep running
eventlet.sleep(10**6)
except Exception as e:
print(f"Error running server: {e}")
def shutdown(self):
print("Shutting down server...")
socketio.stop()
# Note: You might need to stop any running threads/processes here as well.
# To instantiate and run the server
if __name__ == "__main__":
server = WebServer()