-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
54 lines (45 loc) · 1.3 KB
/
index.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
from paddles import Paddles
from time import sleep
from flask import Flask, render_template
from flask_sockets import Sockets
import json
app = Flask(__name__, static_url_path='')
app.debug = True
sockets = Sockets(app)
paddles = Paddles()
button_ws = None
@sockets.route('/button')
def button_socket(ws):
global button_ws
button_ws = ws
paddles.setCurrent(None)
paddles.disable()
paddles.buttonPushedEvent.clear()
paddles.setButtonPressedCb(buttonPressed)
while True:
data = ws.receive()
message = json.loads(data)
if (message['event'] == "reset"):
paddles.reset()
elif (message['event'] == "disable"):
paddles.disable()
def buttonPressed():
global button_ws
print('pressed')
message = {
'event': 'buttonpress',
'paddleId': str(paddles.current.id)
}
button_ws.send(json.dumps(message))
# paddles.setCurrent(None)
# paddles.buttonPushedEvent.clear()
# paddles.enable()
@app.route('/')
def hello():
return app.send_static_file('index.html')
if __name__ == "__main__":
# app.run(host='0.0.0.0')
from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler
server = WSGIServer(("0.0.0.0", 5000), app, handler_class=WebSocketHandler)
server.serve_forever()