1 from flask import Flask, request, render_template
2
3 from geventwebsocket.handler import WebSocketHandler
4 from gevent.pywsgi import WSGIServer
5 from geventwebsocket.websocket import WebSocket
6
7 app = Flask(__name__)
8
9 user_socket_list = []
10
11
12 @app.route('/conn_ws')
13 def ws_app():
14 # print(request.environ)
15
16 user_socket = request.environ.get('wsgi.websocket') # type: WebSocket
17 user_socket_list.append(user_socket)
18 print(len(user_socket_list), user_socket_list)
19 while True:
20 msg = user_socket.receive()
21 print(msg)
22 for i in user_socket_list:
23 i.send(msg)
24
25
26 @app.route('/index')
27 def index():
28 return render_template('my_qserv.html')
29
30
31 if __name__ == '__main__':
32 http_serv = WSGIServer(('0.0.0.0', 9527), app, handler_class=WebSocketHandler)
33
34 http_serv.serve_forever()