Created
May 16, 2012 01:36
-
-
Save stuntgoat/2706612 to your computer and use it in GitHub Desktop.
websocket demo to send multiple messages in Brubeck
This file contains 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
import sys | |
import logging | |
import os | |
from brubeck.request_handling import Brubeck, WebMessageHandler | |
from brubeck.connections import Mongrel2Connection | |
from ws4py.framing import Frame, \ | |
OPCODE_CONTINUATION, OPCODE_TEXT, \ | |
OPCODE_BINARY, OPCODE_CLOSE, OPCODE_PING, OPCODE_PONG | |
class WebsocketHandler(WebMessageHandler): | |
def websocket(self): | |
## DEBUG | |
# print("headers: %s" % self.headers) | |
# print("message: %s" % dir(self.message)) | |
# print("message.headers: %s" % self.message.headers) | |
# print("message.conn_id: %s" % self.message.conn_id) | |
# print("message.cookies: %s" % self.message.cookies) | |
# print("self.application.msg_conn.sender_id: %s" % self.application.msg_conn.sender_id) | |
# print("self.application: %s" % dir(self.application)) | |
[self.application.msg_conn.send(self.application.msg_conn.sender_id, self.message.conn_id, Frame(opcode=OPCODE_TEXT, body=str(message), masking_key=os.urandom(4), fin=1).build()) for message in xrange(100)] | |
message = "I like websockets" | |
ws_frame = Frame(opcode=OPCODE_TEXT, body=message, masking_key=os.urandom(4), fin=1) | |
frame = ws_frame.build() | |
return frame | |
## TO TEST: enter the following in the JavaScript console | |
# > a = new WebSocket('ws://localhost:6767/websockets'); a.onmessage = function (evt) { console.log(evt.data)}; a.onclose = function (evt) {console.log(evt.data)} | |
# > a.send('are you there?') | |
## you should get a list of 100 numbers. Yay! | |
config = { | |
'msg_conn': Mongrel2Connection('tcp://127.0.0.1:9999', 'tcp://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