1

我对 websockets 很陌生,并且一直在尝试将 python 与高速公路一起使用并扭曲连接到多个套接字,然后对来自每个不同套接字的数据执行不同的操作。

最后,我将拥有 3 到 4 个不同的处理功能和多达 10-15 个 websocket 连接。

我想将一个函数传递给协议以用于分析返回的数据。我遇到的问题是我能弄清楚访问 WebSocketClientProtocol 的“onMessage()”方法中的函数的唯一方法是为类而不是实际对象设置的,所以我坚持使用一个数据处理函数适用于所有 websocket。

下面的代码示例。

from autobahn.websocket import WebSocketClientFactory, WebSocketClientProtocol
from twisted.internet import reactor, ssl

# message processing methods
def method1(self, data):
    print 'Using Method 1.'

def method2(self, data):
    print 'Using Method 2.'

# factory and protocol classes
class myFactory(WebSocketClientFactory):
    def clientConnectionLost(self, connector, reason):
        connector.connect()

class myWS(WebSocketClientProtocol):

    def initialize(self, url, data_fcn):
        self.factory = myFactory(url)
        self.factory.protocol = myWS

        # !!! set in class, not object !!!
        self.factory.protocol.data_fcn = data_fcn 

    def onMessage(self, msg, binary):
        self.data_fcn(msg)

    def onClose(self, a, b, c):
        reactor.stop()
        reactor.disconnectAll()

    def kill(self):
        self.transport.loseConnection()
        reactor.stop()
    reactor.disconnectAll()


if __name__ == '__main__':

    # websocket topics unique
    topic_ids = [ '3395',
                  '3563',
                  '3562' ]

    # data processing functions can repeat
    data_process_fcns = [ method1,
                          method2,
                          method1 ]

    for topic, func in zip(topic_ids, data_process_fcns):

        url = 'wss://mywebsocket.com/feed?topic_id[]=' + topic
        ws = myWS()
        ws.initialize(url, func)

        reactor.connectSSL(ws.factory.host, ws.factory.port, ws.factory, ssl.ClientContextFactory())

    reactor.run()

我目前的解决方案是为我想使用的每个数据处理函数创建一个类。所以,对于method1我有

class myWS_method1(WebSocketClientProtocol):

    def onMessage(self, msg, binary):
        method1(msg)

以及我想使用的每种数据处理方法的类似类。

希望找到一个更优雅的解决方案,让我重用一个类。

谢谢,

4

1 回答 1

3

在 Twisted 中,您几乎不会自己构建协议,这是工厂的责任。我想要这样的东西:

class myFactory(WebSocketClientFactory):

    def __init__(self, url, data_processing):
        WebSocketClientFactory.__init__(self, url)
        # here you could have passed whatever parameters
        # later allow you to build up the protocol instance
        self._data_processing = data_processing

    def buildProtocol(self, addr):
        # Use this method to control the creation of the 
        # protocol instance.
        # You can either build a protocol here and inject
        # data_func, or use different protocol class type.

        protocol = myWs()
        protocol.data_func = self._data_processing
        return protocol

比,带有连接的循环看起来像:

for topic, func in zip(topic_ids, data_process_fcns):
    url = 'wss://mywebsocket.com/feed?topic_id[]=' + topic
    factory = myFactory(url, func)
    reactor.connectSSL(factory.host, factory.port, factory, ssl.ClientContextFactory())
于 2013-09-05T09:14:24.500 回答