Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
jkbrzt committed Feb 8, 2012
1 parent 28e1bc2 commit 6faac4c
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions app.py
Expand Up @@ -4,33 +4,6 @@
import redis


HOME_TEMPLATE = """
<!doctype html>
<title>chat</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style>body { max-width: 500px; margin: auto; padding: 1em; background: black; color: #fff; font: 16px/1.6 menlo, monospace; }</style>
<p><b>hi, %s!</b></p>
<p>Message: <input id="in" /></p>
<pre id="out"></pre>
<script>
function sse() {
var source = new EventSource('/stream');
var out = document.getElementById('out');
source.onmessage = function(e) {
out.innerHTML = e.data + '\\n' + out.innerHTML;
};
}
$('#in').keyup(function(e){
if (e.keyCode == 13) {
$.post('/post', {'message': $(this).val()});
$(this).val('');
}
});
sse();
</script>
"""

app = flask.Flask(__name__)
app.secret_key = 'asdf'
red = redis.StrictRedis()
Expand All @@ -48,7 +21,32 @@ def event_stream():
def home():
if 'user' not in flask.session:
return flask.redirect('/login')
return HOME_TEMPLATE % flask.session['user']
return """
<!doctype html>
<title>chat</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<style>body { max-width: 500px; margin: auto; padding: 1em; background: black; color: #fff; font: 16px/1.6 menlo, monospace; }</style>
<p><b>hi, %s!</b></p>
<p>Message: <input id="in" /></p>
<pre id="out"></pre>
<script>
function sse() {
var source = new EventSource('/stream');
var out = document.getElementById('out');
source.onmessage = function(e) {
out.innerHTML = e.data + '\\n' + out.innerHTML;
};
}
$('#in').keyup(function(e){
if (e.keyCode == 13) {
$.post('/post', {'message': $(this).val()});
$(this).val('');
}
});
sse();
</script>
""" % flask.session['user']


@app.route('/login', methods=['GET', 'POST'])
Expand All @@ -60,6 +58,7 @@ def login():
<form action="" method="post">user: <input name="user">
"""


@app.route('/post', methods=['POST'])
def post():
message = flask.request.form['message']
Expand Down

0 comments on commit 6faac4c

Please sign in to comment.