Exemple #1
0
    async def on(self,
                 channel: str,
                 queue: int,
                 func: Callable[[HorseMessage], None],
                 auto_join: bool = True):
        """
        Subscribes to a queue in a channel
        :param channel: Channel name
        :param queue: Queue Id
        :param func: Function that will be called when a message is received
        :param auto_join: If true and client still not joined to channel, joins
        :return:
        """

        subs = next((x for x in self.__subscriptions if not x.direct
                     and x.channel == channel and x.content_type == queue),
                    None)

        if not subs:
            subs = Subscription()
            subs.channel = channel
            subs.content_type = queue
            subs.direct = False
            self.__subscriptions.append(subs)

        subs.actions.append(func)

        if auto_join:
            joined = next((x for x in self.__joined_channels if x == channel),
                          None)
            if not joined:
                await self.join(channel)
Exemple #2
0
    def on_direct(self, content_type: int, func: Callable[[HorseMessage],
                                                          None]):
        """
        Subscribes to all direct messages with specified content type
        :param content_type: Message content type
        :param func: Function that will be called when a message is received
        :return:
        """

        subs = next((x for x in self.__subscriptions
                     if x.direct and x.content_type == content_type), None)
        if not subs:
            subs = Subscription()
            subs.channel = None
            subs.content_type = content_type
            subs.direct = True
            self.__subscriptions.append(subs)

        subs.actions.append(func)