0

我正在尝试按照本教程创建消费者

https://channels.readthedocs.io/en/stable/generics.html

class MyConsumer(WebsocketConsumer):
 def __init__(self,path):
    WebsocketConsumer.__init__(self,path)
    self.client_id_seed = 0
    self.clients_connected = 0
    # The following are passed on to RosbridgeProtocol
    self.fragment_timeout = 600                  # seconds
    self.delay_between_messages = 0              # seconds
    self.max_message_size = None
    self.protocol = None

 def connection_groups(self):
    """
    Called to return the list of groups to automatically add/remove
    this connection to/from.
    """
    return ["test"]

 # Connected to websocket.connect
 def connect(self,message):
    parameters = {
        "fragment_timeout": self.fragment_timeout,
        "delay_between_messages": self.delay_between_messages,
        "max_message_size": self.max_message_size
    }
    try:
        self.protocol.outgoing = self.receive
        self.client_id_seed += 1
        self.clients_connected += 1
    except Exception as exc:

        print ("Unable to accept incoming connection.  Reason: %s" % str(exc))
    print "Client connected.  %d clients total." % self.clients_connected
    self.message.reply_channel.send({"accept": True})


 # Connected to websocket.receive
 def receive(self,text=None, bytes=None):
    # binary = type(message)==bson.BSON
    self.send(text=text, bytes=bytes)

 # Connected to websocket.disconnect
 def disconnect(self,message):
    self.clients_connected -= 1

我在标题中遇到错误,但我已经在构造函数中明确定义了它。disconnect从方法调用错误

4

1 回答 1

0

不应使用这样的构造函数创建消费者。每次请求进入时都会创建一个新的消费者对象。此外,构造函数将消费者发送到等待状态,并且永远不会在其下运行代码。

解决上述错误的最佳方法是将super构造函数移动到__init__.

于 2017-03-21T22:57:24.360 回答