Beispiel #1
0
async def notif_refresh():
    for user, conns in WSR.users.items():
        r = Notification.refresh(user.id)
        c = Notification.count(user.id)

        for ws in conns:
            if not ws.closed:
                await ws.send_json(['notif.refresh', c])
Beispiel #2
0
    async def tick(self):
        """
        定时轮询
        :return:
        """
        data = {}
        now = int(time.time())

        if self.current_user:
            user = self.current_user

            # 检查未读信息
            r = Notification.refresh(user.id)
            c = Notification.count(user.id)
            data['notif_count'] = c

            # 更新在线时间
            await redis.zadd(RK_USER_ACTIVE_TIME_ZSET, now,
                             get_bytes_from_blob(user.id))
        else:
            # TODO: 后面再给auid加几个随机数
            auid = self.params.get('auid', None)
            if auid:
                try:
                    auid = config.LONG_ID_GENERATOR(auid)
                    if not await redis.zscore(RK_USER_ANON_ACTIVE_TIME_ZSET,
                                              auid.to_bin()):
                        auid = None
                except TypeError:
                    auid = None

            if not auid:
                new_id = config.LONG_ID_GENERATOR()
                await redis.zadd(RK_USER_ANON_ACTIVE_TIME_ZSET, now,
                                 new_id.to_bin())
                data['auid'] = new_id.to_hex()
            else:
                await redis.zadd(RK_USER_ANON_ACTIVE_TIME_ZSET, now,
                                 auid.to_bin())

        offset = 30
        data['online'] = await redis.zcount(RK_USER_ACTIVE_TIME_ZSET, min=now - offset) + \
                         await redis.zcount(RK_USER_ANON_ACTIVE_TIME_ZSET, min=now - offset)

        self.finish(RETCODE.SUCCESS, data)