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
<!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> |
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
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)) |
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
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 |