subscription = Template('''
subscription {
users {
id
name
}
}
''')
async def user_listener():
headers = {'Content-Type': 'application/json',
'x-hasura-access-key': 'mysecretkey'}
uri = "ws://localhost:8080/v1alpha1/graphql"
async with websockets.connect(uri, extra_headers=headers) as websocket:
await websocket.send(subscription.render())
while True:
greeting = await websocket.recv()
print(f"< {greeting}")
def main():
asyncio.get_event_loop().run_until_complete(user_listener())
我正在使用python websockets使用Hasura graphql订阅实现一个简单的侦听器。但是,当我运行上述程序时,遇到了以下异常。 (请注意,我正在使用docker-compose在笔记本电脑上尝试过hasura)
File "listener.py", line 39, in <module>
main()
File "listener.py", line 35, in main
asyncio.get_event_loop().run_until_complete(infradb_listener())
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
return future.result()
File "listener.py", line 27, in infradb_listener
async with websockets.connect(uri, extra_headers=headers) as websocket:
File "/Users/vpatil3/projects/hasura-ws-example/venv/lib/python3.6/site-packages/websockets/client.py", line 517, in __aenter__
return await self
File "/Users/vpatil3/projects/hasura-ws-example/venv/lib/python3.6/site-packages/websockets/client.py", line 547, in __await_impl__
extra_headers=protocol.extra_headers,
File "/Users/vpatil3/projects/hasura-ws-example/venv/lib/python3.6/site-packages/websockets/client.py", line 305, in handshake
response_headers, available_subprotocols
File "/Users/vpatil3/projects/hasura-ws-example/venv/lib/python3.6/site-packages/websockets/client.py", line 207, in process_subprotocol
raise InvalidHandshake("no subprotocols supported")
websockets.exceptions.InvalidHandshake: no subprotocols supported
答案 0 :(得分:0)
通过向Websocket Connect API中添加 subproctocols 来解决此问题,如下所示
const total = items.reduce((total, amount) => total + amount.price, 0);