-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathexample_app.py
61 lines (44 loc) · 1.3 KB
/
example_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
import time
from flask import request, Response, render_template, json
from cfcapp import CFCFlask
app = CFCFlask(__name__)
@app.ws_rx_handler
def rx_data(vhost, conn, msg):
say = msg['say']
m = {'from': conn.fid[-8:].upper(), 'content': say}
app.tx(m, bcast=True)
class Robot(object):
" Just a super-simple robot "
def __init__(self):
self.heard = set()
@app.background_task
def robot1():
while 1:
say = "I'm a robot and the time is %s" % time.strftime('%T')
m = {'from': 'Robot1', 'content': say}
app.tx(m, bcast=True)
time.sleep(15)
@app.ws_rx_handler
def rx_data(vhost, conn, msg):
said = msg['say']
if 'robot' in said: return
user = conn.fid[-8:].upper()
if user not in self.heard:
m = {'from': 'Robot1', 'content': "Hello %s. Looking good!" % user}
app.tx(m, bcast=True)
self.heard.add(user)
Robot()
@app.route('/')
def ws_test():
return render_template("chat.html", vhost=request.host)
@app.route('/debug')
@app.route('/debug/<path:whatever>')
def hello_world(whatever=None):
from pprint import pformat
r = pformat(request.environ)
r += "\n\n"
r += pformat(request.environ['werkzeug.request'].__class__.__module__)
r += "\n\n"
r += "URL path: %s\n\n" % request.path
r += "URL: %s" % request.url
return Response(r, mimetype='text/plain')