Created
November 4, 2011 04:49
-
-
Save minikomi/1338681 to your computer and use it in GitHub Desktop.
Barebones chat based on longpolling demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*/*.pid | |
*/*.db | |
*/*.conf |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Long Poller</title> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript" charset="utf-8"></script> | |
<style type="text/css" media="screen"> | |
body { background:#fff; color:#000; font-size:.9em; } | |
#header { background: #333; color: #fff; padding: 10px;} | |
#input_box { background: border: 1px #ccc solid; padding: 10px; width: 100%;} | |
.msg{ background:#aaeeaa; border-bottom: 1px #000 solid; padding: 3px;} | |
</style> | |
<script type="text/javascript" charset="utf-8"> | |
function addmsg(type, response_json) { | |
var msgarray = $.parseJSON(response_json); | |
$("#messages").html(""); | |
for(var i = 0; i < msgarray.length; i++){ | |
$("#messages").prepend( | |
"<div class='msg "+ type +"'>"+ msgarray[i] +"</div>" | |
); | |
} | |
} | |
function waitForMsg() { | |
$.ajax({ | |
type: "GET", | |
url: "/feed", | |
async: true, | |
cache: false, | |
timeout:50000, | |
success: function(data) { | |
addmsg("new", data); | |
setTimeout('waitForMsg()', 1000); | |
}, | |
error: function(XMLHttpRequest, textStatus, errorThrown) { | |
addmsg("error", textStatus + " (" + errorThrown + ")"); | |
setTimeout('waitForMsg()', "15000"); | |
}, | |
}); | |
}; | |
$(document).ready(function(){ | |
waitForMsg(); | |
$("#chat form").submit(function(e){ | |
e.preventDefault(); | |
var success = function(){ | |
$("#msgbox").attr("value", ""); | |
$("#chat .notice").text("Message sent!").show().delay(400).fadeOut(400); | |
}; | |
var message = $("#msgbox").val(); | |
$.ajax({ | |
type: 'POST', | |
url: "/feed", | |
data: {"message": message}, | |
success: success, | |
dataType: "json" | |
}); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<div id="header"> | |
</div> | |
<div id="chat"> | |
<form action="/" method="post" > | |
<input id="msgbox" type="text" name="message" /> | |
<input type="submit" /> | |
<span class="notice"></span> | |
</div> | |
<div id="messages"> | |
</div> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from brubeck.request_handling import Brubeck, WebMessageHandler | |
from brubeck.templating import load_jinja2_env, Jinja2Rendering | |
import sys | |
import datetime | |
import time | |
import json | |
import cgi | |
from gevent.event import Event | |
class DemoHandler(WebMessageHandler, Jinja2Rendering): | |
def get(self): | |
name = self.get_argument('name', 'dude') | |
self.set_body('Take five, %s!' % name) | |
return self.render_template('base.html') | |
class FeedHandler(WebMessageHandler): | |
def post(self): | |
message = self.get_argument('message') | |
if (len(message) > 0): | |
buffer_array.append(cgi.escape(message)) | |
if len(buffer_array) > 20: | |
del buffer_array[0] | |
self.set_body(json.dumps(buffer_array)) | |
new_message_event.set() | |
new_message_event.clear() | |
return self.render() | |
def get(self): | |
new_message_event.wait() | |
self.set_body(json.dumps(buffer_array)) | |
return self.render() | |
config = { | |
'mongrel2_pair': ('ipc://127.0.0.1:9999', 'ipc://127.0.0.1:9998'), | |
'handler_tuples': [(r'^/$', DemoHandler), | |
(r'^/feed', FeedHandler)], | |
'template_loader': load_jinja2_env('./templates/chat'), | |
} | |
new_message_event = Event() | |
buffer_array = ["Hi there! Welcome to barebones chat."] | |
app = Brubeck(**config) | |
app.run() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
brubeck_handler = Handler( | |
send_spec='ipc://127.0.0.1:9999', | |
send_ident='34f9ceee-cd52-4b7f-b197-88bf2f0ec378', | |
recv_spec='ipc://127.0.0.1:9998', | |
recv_ident='') | |
media_dir = Dir( | |
base='media/', | |
index_file='index.html', | |
default_ctype='text/plain') | |
brubeck_host = Host( | |
name="localhost", | |
routes={ | |
'/media/': media_dir, | |
'/': brubeck_handler}) | |
brubeck_serv = Server( | |
uuid="f400bf85-4538-4f7a-8908-67e313d515c2", | |
access_log="/log/mongrel2.access.log", | |
error_log="/log/mongrel2.error.log", | |
chroot="./", | |
default_host="localhost", | |
name="brubeck test", | |
pid_file="/run/mongrel2.pid", | |
port=6767, | |
hosts = [brubeck_host] | |
) | |
settings = {"zeromq.threads": 1} | |
servers = [brubeck_serv] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37366 |
Sure man! Excited to be a part of such an active framework
On Friday, November 4, 2011, James Dennis < ***@***.***> wrote:
This is great! Mind if I hack on it a little bit too? I'd like to add a
splash page that asks a user for a user name and then uses a cookie value
for submission with each chat message.
I'd also like to put it in the demos, but perhaps we can coauthor a full
chat system and give it's own repo too? Would you be into that?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1338681
##
…----=^.^=---
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! Mind if I hack on it a little bit too? I'd like to add a splash page that asks a user for a user name and then uses a cookie value for submission with each chat message.
I'd also like to put it in the demos, but perhaps we can coauthor a full chat system and give it's own repo too? Would you be into that?