[Twisted-Python] Transplating a request's transport (SockJS)

Laurens Van Houtven _ at lvh.cc
Tue Oct 30 05:18:11 EDT 2012

On Tue, Oct 30, 2012 at 10:04 AM, Phil Mayers <p.mayers at imperial.ac.uk>wrote:

> Would it not make more sense to subclass Site and swap out the Channel
> implementation for one that can be "pointed" somewhere else? It means
> you have to use that "Site" for any WebSocket enabled site, but it would
> be clean otherwise. Like so:

Just to be clear (I'm not sure it matters): SockJS is something that gives
a WebSockets API on the client side, but implements that using whatever the
best API is available there. It may in fact be implemented using a whole
host of things, including XHR streaming and polling, iframes, ...

> class WebSockCapableChannel(http.HTTPChannel):
>    def __init__(self):
>      http.HTTPChannel.__init__(self)
>      self.websocket = False
>    def startWebSocket(self, proto):
>      self.websocket = True
>      self.websocket_proto = proto
>      proto.transport = someWrapper(self)
>      proto.connectionMade(...)
>      self.setRawMode()
>    def rawDataReceived(self, data):
>      if self.websocket:
>        self.websocket_proto.dataReceived(data)
>      else:
>        http.HTTPChannel.rawDataReceived(self, data)
> class WebSocketSite(server.Site):
>    procol = WebSockCapableChannel
> ...then in your "Resource" do:
> class MyResource(...):
>    def render(...):
>      request.channel.startWebSocket(someProto())

I'll try that; but isn't render called when an awful lot of parsing has
already been done? Query arguments and headers are already read and parsed;
sockjs-twisted expects its first line to be "POST /echo/bla/bla/bla
HTTP/1.1\r\n" :)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20121030/2c8f43bb/attachment.htm 

More information about the Twisted-Python mailing list