def send_packet(self, packet): logger.debug(format_long("Sending packet: %s", packet)) items = self.parser.encode(packet) binary = False for item in items: self.engine.send(item, binary) binary = True # Attachments are always considered binary
def callback(*args): if callback.sent: logger.warning("Ack already sent") return callback.sent = True logger.debug(format_long("Sending ack: %s", args)) self.send_packet(Packet(type=Packet.ACK, id=id, data=args))
def handle_ack(self, packet): args = packet.data or [] logger.debug(format_long("Received ack: %s", args)) try: callback = self.acks.pop(packet.id) except KeyError: logger.warning("Invalid ack id") else: callback(*args)
def handle_event(self, packet): args = packet.data or [] logger.debug(format_long("Received event: %s", args)) kwargs = {} if packet.id: kwargs['callback'] = self.create_ack_callback(packet.id) self.event_handlers.emit(*args, **kwargs) self.socket_handlers.emit('event', *args, **kwargs)
def emit(self, *args, **kwargs): """Send event-type message If callback is provided, it will be called when the server want to respond to the event (maybe never). """ logger.debug(format_long("Sending event: %s", args)) packet = Packet(type=Packet.EVENT, data=args) callback = kwargs.get('callback', None) if callback: packet.id = self.ids self.acks[packet.id] = callback self.ids += 1 self.send_packet(packet)
def handle_packet(self, packet): if self.state != 'open': return logger.debug(format_long("Received packet: %s", packet)) self.emit('packet', packet)