I have a Twisted webserver serving both a static site and a Autobahn websocket. The websocket is added as a child to the Custom site as below:
self.factory = WebSocketServerFactory(address+":"+str(port), debug=False)
self.factory.protocol = self.getWebSocketProtocol()
resource = WebSocketResource(self.factory)
staticfilepath = kwargs['staticfilepath'].encode('utf-8')
websocketpath = kwargs['websocketpath'].encode('utf-8')
root = CustomFile(staticfilepath)
root.putChild(websocketpath, resource)
This works OK. The problem is that I now need to add the websocket to a nested path (eg instead of at websocketpath="ws"
use websocketpath="sockets/ws"
). I have tried splitting the websocketpath and then creating a resource for each level, adding the websocket to the final but it doesn't seem to work.
I tracked this down to python three string issues in Twisted. If the url is inputted with a trailing slash everything is OK. However if there is no trailing slash then the Twisted function addSlash is called which causes an unhandled exception in Python 3. I put a fix into the Twisted code which is compatible with both python versions and seems to be working OK.