This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (54 loc) · 1.95 KB
/
app.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from geventwebsocket.handler import WebSocketHandler
from geventwebsocket.websocket import WebSocketError
from gevent.pywsgi import WSGIServer
from werkzeug.exceptions import abort
from flask import Flask, request, render_template
import logging
chat_clients = set()
app = Flask(__name__)
app.config['DEBUG'] = True
app.logger.setLevel(logging.INFO)
app.debug = True
@app.route('/')
def index():
return render_template('index.html')
@app.route('/chat')
def chat():
# environ['wsgi.websocket'] から WebSocket オブジェクトが得られる
ws = request.environ['wsgi.websocket']
if ws is None:
app.logger.info("ws is none")
abort(400)
chat_clients.add(ws)
app.logger.info('enter:' + str(len(chat_clients)) + '/' + request.environ['REMOTE_ADDR'] + '/' + str(request.environ['REMOTE_PORT']));
#print 'enter:', len(chat_clients), request.environ['REMOTE_ADDR'], request.environ['REMOTE_PORT']
user_name = request.environ['REMOTE_ADDR'] + ":" + request.environ['REMOTE_PORT']
try:
while True:
# receive message
message = ws.receive()
if message is None:
break;
# broadcast message
app.logger.info(message)
err_clients = set()
for client in chat_clients:
try:
client.send(message)
except Exception:
err_clients.add(client)
for ec in err_clients:
chat_clients.remove(ec)
#except WebSocketError, ex:
except Exception:
app.logger.error('exception')
finally:
# 退室処理
chat_clients.remove(ws)
app.logger.info("bye!")
if __name__ == '__main__':
# WebSocketHandler が environ['wsgi.websocket'] をセットする
http_server = WSGIServer(('', 8080), app, handler_class=WebSocketHandler)
http_server.serve_forever()