Skip to content

Instantly share code, notes, and snippets.

@stuntgoat
Created April 3, 2012 19:52
Show Gist options
  • Save stuntgoat/2295115 to your computer and use it in GitHub Desktop.
Save stuntgoat/2295115 to your computer and use it in GitHub Desktop.
websocket handling for Brubeck
# I am using http://isr.nu/ws/WebSocketTest.htm to help debug my websocket connection
# and I send websocket requests to ws://127.0.0.1:6767/websockets
######## IMPORTANT:
######## Add the websocket method to the MessageHandler class
## inside brubeck.request_handling- add 'websocket' method:
# HTTP_METHODS = ['get', 'post', 'put', 'delete',
# 'head', 'options', 'trace', 'connect', 'websocket']
import sys
import logging
import os
from brubeck.request_handling import Brubeck, WebMessageHandler
import base64, sha
from ws4py.framing import Frame
class WebsocketHandler(WebMessageHandler):
# define websocket method
def websocket(self):
logging.info('headers: %s' % self.message.headers)
ws_frame = Frame(opcode=0x1, body='Hello websockets!', masking_key=os.urandom(4), fin=1)
frame = ws_frame.build()
return frame
config = {
'mongrel2_pair': ('ipc://127.0.0.1:9999', 'ipc://127.0.0.1:9998'),
'handler_tuples': [(r'^/websockets', WebsocketHandler)],
}
app = Brubeck(**config)
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment