如何通过 pyside6 通过 websocket 订阅 binance?
How to subscribe binance via websocket by pyside6?
我可以通过此代码获取流:
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)
打印()输出:PySide6.QtNetwork.QAbstractSocket.SocketState.ConnectingState
self.on_message_received() 没有收到消息。
我不知道为什么...
连接后必须订阅:
import json
from PySide6.QtCore import QObject
from PySide6.QtWebSockets import QWebSocket
class Manager(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.websocket = QWebSocket()
self.websocket.textMessageReceived.connect(self.handle_text_message_received)
self.websocket.connected.connect(self.handle_connected)
def start(self):
url = "wss://stream.binance.com:9443/ws"
self.websocket.open(url)
def subscribe(self):
info = {"method": "SUBSCRIBE", "params": ["btcusdt@aggTrade"], "id": 1}
message = json.dumps(info)
ret = self.websocket.sendTextMessage(message)
assert ret == len(message)
def handle_connected(self):
self.subscribe()
def handle_text_message_received(self, message):
data = json.loads(message)
print(data)
def main():
import sys
from PySide6.QtCore import QCoreApplication
app = QCoreApplication(sys.argv)
manager = Manager()
manager.start()
app.exec()
if __name__ == "__main__":
main()
输出:
{'result': None, 'id': 1}
{'e': 'aggTrade', 'E': 1634398386255, 's': 'BTCUSDT', 'a': 958023628, 'p': '60999.99000000', 'q': '0.00442000', 'f': 1100891458, 'l': 1100891458, 'T': 1634398386255, 'm': True, 'M': True}
{'e': 'aggTrade', 'E': 1634398386750, 's': 'BTCUSDT', 'a': 958023629, 'p': '61000.00000000', 'q': '0.00028000', 'f': 1100891459, 'l': 1100891459, 'T': 1634398386749, 'm': False, 'M': True}
{'e': 'aggTrade', 'E': 1634398386765, 's': 'BTCUSDT', 'a': 958023630, 'p': '61000.00000000', 'q': '0.01447000', 'f': 1100891460, 'l': 1100891461, 'T': 1634398386764, 'm': False, 'M': True}
我可以通过此代码获取流:
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)
打印()输出:PySide6.QtNetwork.QAbstractSocket.SocketState.ConnectingState self.on_message_received() 没有收到消息。
我不知道为什么...
连接后必须订阅:
import json
from PySide6.QtCore import QObject
from PySide6.QtWebSockets import QWebSocket
class Manager(QObject):
def __init__(self, parent=None):
super().__init__(parent)
self.websocket = QWebSocket()
self.websocket.textMessageReceived.connect(self.handle_text_message_received)
self.websocket.connected.connect(self.handle_connected)
def start(self):
url = "wss://stream.binance.com:9443/ws"
self.websocket.open(url)
def subscribe(self):
info = {"method": "SUBSCRIBE", "params": ["btcusdt@aggTrade"], "id": 1}
message = json.dumps(info)
ret = self.websocket.sendTextMessage(message)
assert ret == len(message)
def handle_connected(self):
self.subscribe()
def handle_text_message_received(self, message):
data = json.loads(message)
print(data)
def main():
import sys
from PySide6.QtCore import QCoreApplication
app = QCoreApplication(sys.argv)
manager = Manager()
manager.start()
app.exec()
if __name__ == "__main__":
main()
输出:
{'result': None, 'id': 1}
{'e': 'aggTrade', 'E': 1634398386255, 's': 'BTCUSDT', 'a': 958023628, 'p': '60999.99000000', 'q': '0.00442000', 'f': 1100891458, 'l': 1100891458, 'T': 1634398386255, 'm': True, 'M': True}
{'e': 'aggTrade', 'E': 1634398386750, 's': 'BTCUSDT', 'a': 958023629, 'p': '61000.00000000', 'q': '0.00028000', 'f': 1100891459, 'l': 1100891459, 'T': 1634398386749, 'm': False, 'M': True}
{'e': 'aggTrade', 'E': 1634398386765, 's': 'BTCUSDT', 'a': 958023630, 'p': '61000.00000000', 'q': '0.01447000', 'f': 1100891460, 'l': 1100891461, 'T': 1634398386764, 'm': False, 'M': True}