【问题标题】:Websocket connection with autobahn and twisted in pythonWebsocket与高速公路的连接并在python中扭曲
【发布时间】:2014-11-18 11:51:58
【问题描述】:

我正在尝试使用 twiested with autobahn 连接到 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(),工厂就会关闭

【问题讨论】:

  • 您解决过这个问题吗?如果有,是如何解决的?
  • 不,我已经放弃它并尝试了其他解决方案。

标签: python websocket twisted autobahn


【解决方案1】:

reactor.connectTCP 接受 IP 地址(或主机名)作为其第一个参数。你向它传递了一个 URI。它感到困惑并决定这一定是一个主机名,尝试解决它,但失败了,并停止了客户端工厂。

尝试传递 real.okcoin.com 而不是完整的 URI。这可以解析为一个 IP 地址(我假设),并且连接尝试将能够继续进行。

【讨论】:

猜你喜欢
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多