从 Coinbase Exchange API Websocket 接收数据
Receiving data from Coinbase Exchange API Websocket
我正在尝试使用 Coinbase Exchange API 构建订单簿快照,特别是使用 Websocket 提要。
https://docs.exchange.coinbase.com/?python#websocket-feed
我相信我已成功建立连接并发送初始订阅消息。在初始订阅消息之后,我期待 onMessage 事件。但是似乎没有这样的消息到达。最终连接超时并关闭。
代码:
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS
import json
class ClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
print("Server connected: {0}".format(response.peer))
def initMessage(self):
message_data = [{"type": "subscribe", "product_id": "BTC-USD"}]
message_json = json.dumps(message_data)
print "sendMessage: " + message_json
self.sendMessage(message_json)
def onOpen(self):
print "onOpen calls initMessage()"
self.initMessage()
def onMessage(self, msg, binary):
print "Got echo: " + msg
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if __name__ == '__main__':
factory = WebSocketClientFactory("wss://ws-feed.exchange.coinbase.com")
factory.protocol = ClientProtocol
connectWS(factory)
reactor.run()
输出:
python orders_twisted.py
服务器连接:tcp4:190.93.242.231:443
onOpen 调用 initMessage()
sendMessage: [{"type": "subscribe", "product_id": "BTC-USD"}]
WebSocket 连接关闭:连接关闭不干净(对端在没有先前的 WebSocket 关闭握手的情况下丢弃了 TCP 连接)
您发送的订阅请求是作为 JSON 字典数组发送的,而应该只是字典。更改代码自:
def initMessage(self):
message_data = [{"type": "subscribe", "product_id": "BTC-USD"}]
message_json = json.dumps(message_data)
...
至:
def initMessage(self):
message_data = {"type": "subscribe", "product_id": "BTC-USD"}
message_json = json.dumps(message_data)
...
通过此更改,您的代码已成功订阅 ...
我正在尝试使用 Coinbase Exchange API 构建订单簿快照,特别是使用 Websocket 提要。
https://docs.exchange.coinbase.com/?python#websocket-feed
我相信我已成功建立连接并发送初始订阅消息。在初始订阅消息之后,我期待 onMessage 事件。但是似乎没有这样的消息到达。最终连接超时并关闭。
代码:
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS
import json
class ClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
print("Server connected: {0}".format(response.peer))
def initMessage(self):
message_data = [{"type": "subscribe", "product_id": "BTC-USD"}]
message_json = json.dumps(message_data)
print "sendMessage: " + message_json
self.sendMessage(message_json)
def onOpen(self):
print "onOpen calls initMessage()"
self.initMessage()
def onMessage(self, msg, binary):
print "Got echo: " + msg
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if __name__ == '__main__':
factory = WebSocketClientFactory("wss://ws-feed.exchange.coinbase.com")
factory.protocol = ClientProtocol
connectWS(factory)
reactor.run()
输出:
python orders_twisted.py 服务器连接:tcp4:190.93.242.231:443 onOpen 调用 initMessage() sendMessage: [{"type": "subscribe", "product_id": "BTC-USD"}] WebSocket 连接关闭:连接关闭不干净(对端在没有先前的 WebSocket 关闭握手的情况下丢弃了 TCP 连接)
您发送的订阅请求是作为 JSON 字典数组发送的,而应该只是字典。更改代码自:
def initMessage(self):
message_data = [{"type": "subscribe", "product_id": "BTC-USD"}]
message_json = json.dumps(message_data)
...
至:
def initMessage(self):
message_data = {"type": "subscribe", "product_id": "BTC-USD"}
message_json = json.dumps(message_data)
...
通过此更改,您的代码已成功订阅 ...