问题描述
我正在尝试使用 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 模块,但运气不佳。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)