i can get stream by this code:
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)
this can received stream normally, but i want to use subscribe, so i write this:
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 and self.on_message_received() no message received.
i dont know why...
CodePudding user response:
You have to subscribe after connecting:
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()
Output:
{'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}
CodePudding user response:
this code can get stream:
import sys
import json
from PySide6.QtWidgets import QApplication, QDialog
from PySide6.QtWebSockets import QWebSocket
class Main_Dlg(QDialog):
def __init__(self):
super(Main_Dlg, self).__init__()
self.web_socket = QWebSocket()
self.web_socket_url = 'wss://stream.binance.com:9443/ws'
# self.web_socket.open(self.web_socket_url)
self.web_socket.open(self.web_socket_url '/btcusdt@kline_5m/bnbusdt@kline_5m')
# sub_info = {
# "method": "SUBSCRIBE",
# "params":
# [
# "btcusdt@aggTrade"
# ],
# "id": 1
# }
# ret = self.web_socket.sendTextMessage(json.dumps(sub_info))
self.web_socket.textMessageReceived.connect(self.on_message_received)
def on_message_received(self, message):
print(message)
if __name__ == '__main__':
app = QApplication(sys.argv)
dlg = Main_Dlg()
# dlg.show()
sys.exit(app.exec())
but replaced by commented code, can not send and get subscribed stream.