[Twisted-web] Support for websockets.

Mike Mestnik cheako+twistedmatrix at mikemestnik.net
Fri Aug 4 08:01:36 MDT 2017


I was working on Tor2web, a client of twisted web.  I discover that to
move forward I'd need
[iResponce](https://github.com/racker/python-twisted-web/blob/master/twisted/web/iweb.py#L497)
to expose the socket or something to read/write on.  Once it's
necessary to edit twisted-web I might as well go all out and at least
partially implement Websockets.

Here is what I did for [Tor2web](https://pastebin.com/MbHVvwNz) It's
incomplete, I was reading twisted-web and discovered the masked
interface.  Here is a [good reference for
Websockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers)
at a protocol level, it does say server but if you'r not working in
Javascript this is the document u need to read.

Here is what I'd envision for an interface, knowing only a little
Python and almost no twisted.

1. Upon receiving the response headers indicating a change of
protocol(connection: upgrade).  Remove the socket from any connection
pool, as it can't ever be used for http.  The connection will be
upgraded until it's closed.

2. Expose a socket interface in the iResponse class, it could be the
class itself or an attribute.  This could be a copy of the twisted
socket interface or even that interface itself.  With at least.
a. onRead callback.
b. Write.
c. Close.
d. onClose callback.
e. onError, ect.

The api does not NEED a method to determine if the connection was
upgraded, since the headers indicate as much.  However to prevent
attempts to access the Websocket API when it's not available a
python(ish) method for detecting as much I'll leave to your
discretion.

Thank you.



More information about the Twisted-web mailing list