简体   繁体   English

将Autobahn Websocket添加到嵌套的Twisted资源中

[英]Add an Autobahn websocket to a nested Twisted resource

I have a Twisted webserver serving both a static site and a Autobahn websocket. 我有一个同时服务于静态站点和高速公路站点的Twisted网络服务器。 The websocket is added as a child to the Custom site as below: Websocket作为子级添加到“定制”站点,如下所示:

    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" ). 问题是我现在需要将websocket添加到嵌套路径中(例如,代替websocketpath="ws"使用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. 我试过拆分websocket路径,然后为每个级别创建一个资源,将websocket添加到最终版本,但似乎不起作用。

I tracked this down to python three string issues in Twisted. 我在Twisted中将此问题归结为python三个字符串问题。 If the url is inputted with a trailing slash everything is OK. 如果在URL后面加上斜杠,则一切正常。 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. 但是,如果没有尾部的斜杠,则会调用Twisted函数addSlash,这会在Python 3中引起未处理的异常。我在Twisted代码中添加了一个修复程序,该代码与两个python版本都兼容,并且似乎可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM