我想要连接到这个website后面的网络插座(点击一个球员,这样高尔夫球场就可以看到了).当我判断客户端和服务器之间的流量时,我看到第一条消息是握手:
{"type": "connection_init",
"payload":
{
"accept-language": "en",
"ec-version": "5.1.88",
"sport": "GOLF",
"referrer": "https://www.europeantour.com/",
"operator": "europeantour"
}
}
根据格式(DICT的关键字),我得出结论,WebSocket使用了Apollo WebSocket传输协议.
接下来,我将按照gql
‘S的WebSocket-example文档进行操作.
import asyncio
import logging
from gql import gql, Client
from gql.transport.websockets import WebsocketsTransport
logging.basicConfig(level=logging.INFO)
async def main():
transport = WebsocketsTransport(url='wss://btec-websocket.services.imgarena.com',
init_payload={'accept-language': 'en',
'ec-version': '5.1.88',
'operator': 'europeantour',
'referrer': 'https://www.europeantour.com/',
'sport': 'GOLF'})
async with Client(transport=transport,
fetch_schema_from_transport=False) as session:
# do something
asyncio.run(main())
在阅读了更多关于协议here的内容后,我仍然不明白如何在我的Python
脚本中向服务器发送消息
如何将下面的消息发送到WebSocket?
{
"id": "1073897396",
"type": "start",
"operationName": "SubscribeToGolfTournamentStatus",
"eventId": 488
}
Edit个
使用@Vonc的建议的不同方法
import asyncio
import logging
from gql import gql, Client
from gql.transport.websockets import WebsocketsTransport
logging.basicConfig(level=logging.INFO)
async def main():
transport = WebsocketsTransport(url='wss://btec-websocket.services.imgarena.com',
init_payload={
'accept-language': 'en',
'ec-version': '5.1.88',
'operator': 'europeantour',
'referrer': 'https://www.europeantour.com/',
'sport': 'GOLF'
})
async with Client(transport=transport, fetch_schema_from_transport=False) as session:
# Define the subscription
subscription = gql('''
subscription MapVisualisationSubscribeTo3DShots($input: SubscribeToGolfTeam3DShotsInput!) {
subscribeToGolf3DShots(input: $input) {
__typename
x
y
z
addressing
provisionalIndex
ballHoled
isApproximate
strokeNo
holeNo
surfaceTypeCode
teamId
courseId
seqNum
isBallDrop
isProvisional
isProvisionalSelected
shotDistance
score
distanceToPin
player {
id
}
}
}
''')
# Subscribe and handle incoming data
async for result in session.subscribe(subscription, variable_values={"input": {"tournamentId": 669,
"holeNo": 1,
"roundNo": 4,
"matchNo": 4001,
"holeOrder": 1
}
}):
print(result)
asyncio.run(main())
以上是基于在Network
中观察到的以下WebSocket消息
{"id":"2097853415","type":"full-subscription","payload":{"variables":{"input":{"tournamentId":669,"holeNo":1,"roundNo":4,"matchNo":4001,"holeOrder":1}},"query":"\n subscription MapVisualisationSubscribeTo3DShots($input: SubscribeToGolfTeam3DShotsInput!) {\n subscribeToGolf3DShots(input: $input) {\n __typename\n x\n y\n z\n addressing\n provisionalIndex\n ballHoled\n isApproximate\n strokeNo\n holeNo\n surfaceTypeCode\n teamId\n courseId\n seqNum\n isBallDrop\n isProvisional\n isProvisionalSelected\n shotDistance\n score\n distanceToPin\n player {\n id\n }\n }\n }\n","operationName":"MapVisualisationSubscribeTo3DShots","subscriptionName":"subscribeToGolf3DShots"}}