Skip to content

Instantly share code, notes, and snippets.

@zeekay
zeekay / gist:1221762
Created September 16, 2011 10:16
index.tpl
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Chat</title>
<link rel="stylesheet" href="/static/style.css">
<script type="text/javascript" src="/static/web-socket-js/swfobject.js"></script>
@zeekay
zeekay / gist:1221757
Created September 16, 2011 10:13
chat.py
from bottle import route, request, run, static_file, template
from bottle_websocket import websocket, GeventWebSocketServer
from collections import defaultdict, deque
class ChatRoom(object):
users = {}
buffer = deque(maxlen=15)
def message(self, name, message):
self.buffer.append((name, message))
@zeekay
zeekay / gist:1221752
Created September 16, 2011 10:12
bottle_websocket.py
from bottle import ServerAdapter
from bottle import request
import json
def websocket(callback):
def wrapper(*args, **kwargs):
ws = request.environ['wsgi.websocket']
ws.json = lambda x: ws.send(json.dumps(x))
rv = callback(ws, *args, **kwargs)
return rv