【问题标题】:WAMP websocket server connection error on Python with Autobahn使用高速公路的 Python 上的 WAMP websocket 服务器连接错误
【发布时间】: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


    【解决方案1】:

    这看起来像是网络服务器问题。我的理解是 websocket 是通过升级常规 HTTP 连接创建的。

    您需要配置 Web 服务器,以及中间的任何代理,以升级配置。

    我在 nginx 中遇到过这种情况。这看起来像 good example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-06
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多