繁体   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