我可以通过以下代码获得流:
from PySide6.QtWebSockets import QWebSocket
...
self.websocket = QWebSocket()
self.websocket_base_url = 'wss://stream.binance.com:9443/ws'
self.websocket.open(self.websocket_base_url + '/btcusdt@kline_5m')
self.websocket.textMessageReceived.connect(self.on_message_received)
def on_message_received(self, msg):
print(msg)
这可以正常接收流,但我想使用订阅,所以我写了这个:
self.websocket = QWebSocket()
self.websocket_base_url = 'wss://stream.binance.com:9443/ws'
self.websocket.open(self.websocket_base_url)
info = {
"method": "SUBSCRIBE",
"params":
[
"btcusdt@aggTrade"
],
"id": 1
}
ret = self.websocket.sendTextMessage(json.dumps(info))
print(str(self.websocket.state()))
self.websocket.textMessageReceived.connect(self.on_message_received)
def on_message_received(self, msg):
print(msg)
print()output:PySide6.QtNetwork.QAbstractSocket.SocketState.ConnectingState和self.on_message_received()未收到消息。
我不知道为什么……
1条答案
按热度按时间zpqajqem1#
您必须在连接后订阅:
输出: