Commits

Anonymous committed 35f12a3

first commit

Comments (0)

Files changed (4)

+syntax: glob
+
+*.coverage
+*.egg-info
+*.log
+*.pyc
+*.db
+*.swp
+*.swo
+*.zip
+*.orig
+*.cfg
+*~
+
+fabfile.py
+producton_settings.py
+
+dist
+docs/output
+
+
+from gevent import monkey
+from gevent.event import Event
+from gevent.wsgi import WSGIServer
+
+from flask import Flask, jsonify, render_template, request
+
+monkey.patch_socket()
+
+DEBUG = True
+
+app = Flask(__name__)
+app.config.from_object(__name__)
+
+event = Event()
+messages = []
+current_length = 0
+
+@app.route("/")
+def index():
+    """
+    Display the page containing chat room
+    """
+    return render_template("index.html")
+
+
+@app.route("/new/", methods=("POST",))
+def new_message():
+    """
+    Create a new message
+    """
+    global current_length
+
+    body = request.form['body'].strip()
+    if body:
+        event.set()
+        event.clear()
+    
+        messages.append(request.form['body'])
+        current_length = len(messages)
+
+    return jsonify(messages=messages)
+
+
+@app.route("/update/", methods=("POST",))
+def update_messages():
+    """
+    Return list of updated messages
+    """
+    if not messages or len(messages) == current_length:
+        event.wait()
+    return jsonify(messages=messages)
+
+
+if __name__ == "__main__":
+    WSGIServer(('', 8080), app.wsgi_app).serve_forever()

static/chatbox.js

+var ChatBox = function(options)
+{
+    var _this = this;
+    _this.pollUrl = options.pollUrl;
+    _this.newMessageUrl = options.newMessageUrl;
+    $('#messageform').submit(function() { _this.newMessage(); return false; });
+    _this.poll();
+}
+
+ChatBox.prototype.onSuccess = function(response)
+{
+    if (!response) return;
+
+    var _this = this;
+    html = []
+    for (var i=0; i < response.messages.length; i++){
+        html.push('<li>' + response.messages[i] + '</li>');
+    }
+    $('#messages').html(html.join(""));
+    setTimeout(function(){  _this.poll(); }, 5000);
+}
+
+ChatBox.prototype.poll = function()
+{
+    var _this = this;
+    
+    $.post(_this.pollUrl, null, function(response) { _this.onSuccess(response); }, "json");
+}
+
+ChatBox.prototype.newMessage = function()
+{
+    var _this = this;
+
+    var message = $('#message').val();
+    var params = { body : message };
+
+    $.post(_this.newMessageUrl, params, function(response) { _this.onSuccess(response); $('#message').val(''); }, "json");
+    
+}

templates/index.html

+<!DOCTYPE HTML>
+<html lang="en">
+<head>
+	<meta charset="UTF-8">
+	<title>ChatBox</title>
+    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
+    <script type="text/javascript" src="{{ url_for('.static', filename='chatbox.js') }}"></script>
+</head>
+<body>
+    <h1>ChatBox chat room</h1>
+    <ul id="messages"></ul>
+    <form id="messageform">
+          <input name="body" id="message" style="width:500px"/>
+		<input type="submit" value="Post"/>
+	</form>
+    <script type="text/javascript">
+        $(document).ready(function(){
+        var cb = new ChatBox({
+            newMessageUrl : '{{ url_for("new_message") }}',
+            pollUrl : '{{ url_for("update_messages") }}'
+        })
+        });
+    </script>
+</body>
+</html>