簡體   English   中英

Websocket連接與高速公路並在python中扭曲

[英]Websocket connection with autobahn and twisted in python

我正在嘗試使用帶高速公路的twiested連接到Websocket服務器

from autobahn.twisted.websocket import WebSocketClientProtocol


class OkcClient(WebSocketClientProtocol):
    def onOpen(self):
        #self.sendMessage(u"Hello, world!".encode('utf8'))
        self.sendMessage(u"{'event':'addChannel','channel':'ok_btcusd_future_ticker_this_week'}".encode('utf8'))
        self.sendMessage(u"{'event':'addChannel','channel':'ok_future_btcusd_kline_this_week_5min'}".encode('utf8'))

    def onMessage(self, payload, isBinary):
        if isBinary:
            print("Binary message received: {0} bytes".format(len(payload)))
        else:
            print("Text message received: {0}".format(payload.decode('utf8')))


import sys
from twisted.python import log
from twisted.internet import reactor
from autobahn.twisted.websocket import WebSocketClientFactory
log.startLogging(sys.stdout)
factory = WebSocketClientFactory("wss://real.okcoin.com:10440/websocket/okcoinapi")
factory.protocol = OkcClient

reactor.connectTCP("wss://real.okcoin.com/websocket/okcoinapi", 10440, factory)
reactor.run()

但是我唯一得到的是消息:

2014-11-18 11:45:39+0000 [-] Log opened.
2014-11-18 11:45:51+0000 [-] Starting factory <autobahn.twisted.websocket.WebSocketClientFactory instance at 0x106a0ccf8>
2014-11-18 11:46:04+0000 [-] Stopping factory <autobahn.twisted.websocket.WebSocketClientFactory instance at 0x106a0ccf8>

無論我嘗試了什么,只要我做一個reactor.run() ,工廠就會關閉。

reactor.connectTCP接受IP地址(或主機名)作為其第一個參數。 您為其傳遞了URI。 它感到困惑,並決定這必須是一個主機名,試圖解析它,失敗了,並停止了客戶端工廠。

嘗試傳遞real.okcoin.com而不是完整的URI。 可以將其解析為IP地址(我認為),並且連接嘗試將能夠繼續進行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM