-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
32 lines (25 loc) · 862 Bytes
/
server.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
from flask import Flask, render_template
from flask_socketio import SocketIO
import threading
import queue
messages = queue.Queue()
def send(message, data):
messages.put({'message': message, 'data': data})
app = Flask(__name__, static_url_path='', static_folder='public')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
# liest Messages aus der queue und sendet sie als Websockets
def background_thread():
count = 0
while True:
socketio.sleep()
if not messages.empty():
message = messages.get()
socketio.emit(message['message'], message['data'])
def thread_function(name):
socketio.start_background_task(background_thread)
socketio.run(app)
def start():
thread = threading.Thread(target=thread_function, args=(1,))
thread.start()
print("server started on port 5000")