【发布时间】:2021-03-23 16:46:31
【问题描述】:
我正在尝试使用 Python 连接到 WAMP websocket 服务器并订阅以接收消息,但我无法实现。我已经设法在 JS 上使用 Autobahn 与此代码进行连接:
< script src = "autobahn.js" > < /script> <script >
var conn = new ab.Session('ws://examplehost.com:8443/ws',
function() {
conn.subscribe('channel', function(topic, data) {
console.log(data);
alert('New data arrived: "' + topic + '" : ' + data.title);});},
function() {
console.warn('WebSocket connection closed');
}, {'skipSubprotocolCheck': true});
</script>
但是在 Python 上使用相同的库,下面的代码会导致 404 错误:
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
class Component(ApplicationSession):
async def onJoin(self, details):
def on_event(i):
print("New data arrived: {}".format(i))
await self.subscribe(on_event, 'channel')
if __name__ == '__main__':
url = "ws://examplehost.com:8443/ws"
runner = ApplicationRunner(url)
runner.run(Component)
这是我得到的错误:
failing WebSocket opening handshake ('WebSocket connection upgrade failed (404 - NotFound)')
dropping connection to peer tcp4:123.123.123.123:8443 with abort=True: WebSocket connection upgrade failed (404 - NotFound)
关于如何解决这个问题的任何想法?经过大量搜索后,可能在服务器上使用路径/ws 会导致一些问题,但我不确定。我也尝试过许多其他 Python 模块,但运气不佳。
【问题讨论】:
标签: javascript python websocket autobahn wamp-protocol