Пример #1
0
    async def process_update(self, websocket, path):
        user = await self.register(websocket)
        try:
            while True:
                request = await websocket.recv()
                data = {}

                User.set_current(user)
                #Channel.set_current(user.get_channel())

                logger.debug(f"current user {User.get_current()}")
                #logger.debug(f"current channel {Channel.get_current()}")

                try:
                    data = json.loads(request)
                    request_object = self.dispatcher.parse_request(data)
                    WebsocketEvent.set_current(request_object)
                except TypeError as e:
                    logger.exception(
                        f"Failed to parse input message: {data} with error: {e}"
                    )
                    continue
                else:
                    #notify all handlers
                    logger.debug(f"Received {request_object}")
                    try:
                        await self.dispatcher.feed_request(request_object)
                    except Exception as e:
                        logger.exception(
                            f"Cause exception while process {request_object}: {e}"
                        )
        except Exception as e:
            logger.info(f"user disconected with {e}")
        finally:
            await self.unregister(websocket)
async def echo(event: WebsocketEvent, data):
    logger.info('chat')
    logger.info(event)
    await s.mongo.insert_message(event.body)
    logger.info(event.body)
    channel = User.get_current().get_channel()
    logger.info(channel)
    await channel.to_all_users(event.body)
async def echo(event: WebsocketEvent, data):
    logger.info('message list history')
    logger.info(event)
    user = User.get_current()
    channel = user.get_channel()

    message_history = await s.mongo.get_message_for_channel(channel.id, page = event.body)
    await asyncio.sleep(4)
    await event.answer(message_history)
async def echo(event: WebsocketEvent, data):
    logger.info('status')
    logger.info(event)
    user = User.get_current()
    channel = user.get_channel()
    user.status = event.body['status']
    if (user.status == -1):
        await s.mongo.change_user_list_channel(user.on_channel_id, user.to_dict(), False)
    await channel.to_all_users(user.to_dict())
async def echo(event: WebsocketEvent, data):
    logger.info('login')
    s.ch_pool.add_channel(int(event.body['channelId']))
    user = User.get_current()
    user.avatar = event.body['avatar']
    user.status = event.body['status']

    res = user.move_to_channel(int(event.body['channelId']))
    channel = user.get_channel()
    if (user.id != -1):
        await s.mongo.change_user_list_channel(int(event.body['channelId']), user.to_dict(), True)

    await user.custom_answer('UserList', channel.user_list_except(user.id))
    message_history = await s.mongo.get_message_for_channel(channel.id, page = 1)
    logger.debug(message_history)
    await user.custom_answer('MessageListHistory', message_history)

    await channel.to_all_users(user.to_dict())
Пример #6
0
    async def _process_event(self, request, data: dict):
        logger.debug(f"processing event {request}")
        try:
            if request.token:
                decoded = decode(request.token)
                u = User.get_current()
                u.id = decoded['id']
                u.login = decoded['login']
                u.role = decoded['role']
        except Exception as e:
            logger.debug("invalid token - {} error {}".format(token, e))
            raise SkipHandler()

        if request.event == "Login":
            result = await self.login_handler.notify(request, data)
            logger.info("Da Robe")
        elif request.event == "GetInfo":
            result = await self.get_info_handler.notify(request, data)
        elif request.event == "Ping":
            result = await self.ping_handler.notify(request, data)
        elif request.event == "GetChannels":
            result = await self.get_channel_handler.notify(request, data)
        elif request.event == "MoveToChannel":
            result = await self.move_to_channel_handler.notify(request, data)
        elif request.event == "ChatMessage":
            result = await self.chat_message_handler.notify(request, data)
        elif request.event == "ChangeStatus":
            result = await self.change_status_handler.notify(request, data)
        elif request.event == "MessageListHistory":
            result = await self.message_list_history.notify(request, data)
        elif request:
            result = await self.unhandled_event.notify(request, data)
        else:
            raise SkipHandler()
        if result:
            return result
        raise SkipHandler()
async def echo(event: WebsocketEvent, data):
    res = User.get_current().move_to_channel(int(event.body))
    logger.info(User.get_current().get_channel())
    await event.answer({"moved": res})