-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathevent_server.py
130 lines (104 loc) · 3.37 KB
/
event_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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Benjamin Milde'
import flask
from flask_cors import CORS
import redis
import os
import json
import bs4
#import bridge
import codecs
import datetime
from werkzeug.serving import WSGIRequestHandler
base_path = os.getcwd() + '/example/'
server_channel = 'asr'
decode_control_channel = 'asr_control'
app = flask.Flask(__name__)
app.secret_key = 'asdf'
app._static_folder = base_path
app._static_files_root_folder_path = base_path
CORS(app)
red = redis.StrictRedis()
long_poll_timeout = 0.5
long_poll_timeout_burst = 0.08
#Send event to the event stream
def event_stream():
print("New connection to event_stream!")
pubsub = red.pubsub()
pubsub.subscribe(server_channel)
yield b'hello'
for message in pubsub.listen():
if not message['type'] == 'subscribe':
#print('New message:', message)
#print(type(message['data']))
yield b'data: %s\n\n' % message['data']
@app.route('/reset')
def reset():
red.publish(decode_control_channel, 'reset')
print("reset called")
return 'OK'
@app.route('/stop')
def stop():
red.publish(decode_control_channel, 'stop')
print("stop called")
return 'OK'
@app.route('/start')
def start():
red.publish(decode_control_channel, 'start')
print("start called")
return 'OK'
@app.route('/shutdown')
def shutdown():
red.publish(decode_control_channel, 'shutdown')
print("shutdown called")
return 'OK'
@app.route('/status')
def status():
red.publish(decode_control_channel, 'status')
print("status called")
return 'OK'
@app.route('/reset_timer')
def reset_timer():
red.publish(decode_control_channel, 'reset_timer')
print("reset time called")
return 'OK'
#Event stream end point for the browser, connection is left open. Must be used with threaded Flask.
@app.route('/stream')
def stream():
return flask.Response(event_stream(), mimetype="text/event-stream")
#Traditional long polling. This is the fall back, if a browser does not support server side events. TODO: test and handle disconnects
@app.route('/stream_poll')
def poll():
pubsub = red.pubsub()
pubsub.subscribe(server_channel)
message = pubsub.get_message(timeout=long_poll_timeout)
while(message != None):
yield message
message = pubsub.get_message(timeout=long_poll_timeout_burst)
#These should ideally be served with a real web server, but for developping purposes, serving static files with Flask is also ok:
#START static files
@app.route('/')
def root():
print('root called')
# return app.send_static_file(base_path+'index.html')
return flask.send_from_directory(base_path, 'index.html')
@app.route('/css/<path:path>')
def send_css(path):
return flask.send_from_directory(base_path+'css', path)
@app.route('/js/<path:path>')
def send_js(path):
return flask.send_from_directory(base_path+'js', path)
@app.route('/pics/<path:path>')
def send_pics(path):
return flask.send_from_directory(base_path+'pics', path)
@app.route('/fonts/<path:path>')
def send_fonts(path):
return flask.send_from_directory(base_path+'fonts', path)
# END static files
if __name__ == '__main__':
print(' * Starting app with base path:',base_path)
# new_session_outfile()
app.debug = True
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app.run(host='0.0.0.0', threaded=True, port=5000)