当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript ServerApp.service方法代码示例

本文整理汇总了TypeScript中@feathersjs/feathers.ServerApp.service方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ServerApp.service方法的具体用法?TypeScript ServerApp.service怎么用?TypeScript ServerApp.service使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在@feathersjs/feathers.ServerApp的用法示例。


在下文中一共展示了ServerApp.service方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: onJoin

export default function onJoin(
  app: ServerApp,
  connection: SocketConnection,
): SocketEventHandler<'queues:join'> {
  const queues = app.service('queues');
  const players = app.service('players');

  return async (data, cb) => {
    const { user } = connection.feathers;

    // Make sure a userId is authenticated
    if (!user) {
      return cb(new NotAuthenticated('You aren\'t authenticated!'));
    }

    const {
      region,
      id: userId,
    } = user;
    const queueId = `${region}-${data.gamemode}`;

    try {
      const queue = await queues.get(queueId);
      const player = await getPlayer(app, queueId, userId);

      if (player) {
        await players.patch(player.id, { class: data.class });
      } else {
        await players.create({
          id: new mongoose.Types.ObjectId().toHexString(),
          userId,
          queueId,
          map: null,
          isReady: queue.state === 'ready-up',
          isSubbed: false,
          pickupId: null,
          joinedOn: Date.now(),
          class: data.class,
        });
      }

      await checkForUpdateState(app, queue);

      return cb(null);
    } catch (error) {
      log('Error while player wanted to join pickup queue', {
        error,
        userId,
        data,
      });

      return cb(error);
    }
  };
}
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:55,代码来源:on-join.ts

示例2: async

  app.on('login', async (_, { connection }) => {
    const userId = connection.user.id;

    try {
      const profile = await app.service('user-profiles').get(userId);
      const data = await getUserData(profile);

      await app.service('user-profiles').patch(userId, data);
    } catch (error) {
      log('Error in login callback', error);
    }
  });
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:12,代码来源:setup-events.ts

示例3: setupQueue

async function setupQueue(
  app: ServerApp,
  region: keyof typeof regions,
  gamemode: keyof typeof gamemodes,
) {
  const queues = app.service('queues');
  const id = `${region}-${gamemode}`;

  try {
    await queues.patch(id, {
      state: QueueStates.WaitingForPlayers,
      readyUpEnd: null,
    });
  } catch (error) {
    if (error.code === 404) {
      await queues.create({
        id,
        region,
        gamemode,
        state: QueueStates.WaitingForPlayers,
        maps: getRandomMaps(id, null),
        readyUpEnd: null,
      });
    }
  }
}
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:26,代码来源:setup-queues.ts

示例4: handleReadyUpTimeout

async function handleReadyUpTimeout(app: ServerApp, queueId: string) {
  const queues = app.service('queues');

  try {
    const queue = await queues.get(queueId);
    const hasEnough = await hasEnoughPlayers(
      app,
      queue,
      // Check if the enough players are ready
      players => players.filter(player => player.isReady).length,
    );

    if (hasEnough) {
      await queues.patch(queueId, {
        state: QueueStates.CreatingPickup,
        readyUpEnd: null,
      });
    } else {
      await resetQueue(app, queueId);
    }
  } catch (error) {
    log('Error while handling end of ready up timeout', {
      error,
      data: { queueId },
    });

    await resetQueue(app, queueId);
  }
}
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:29,代码来源:handle-ready-up-timeout.ts

示例5: createNewPickup

async function createNewPickup(
  app: ServerApp,
  region: keyof typeof regions,
  gamemode: keyof typeof gamemodes,
  map: keyof typeof maps,
) {
  const pickups = app.service('pickups');

  try {
    const [lastPickup] = await pickups.find({ query: { $sort: { id: 1 } } });

    const pickup = await pickups.create({
      id: lastPickup ? lastPickup.id + 1 : 1,
      map,
      region,
      gamemode,
      state: PickupStates.ReservingServer,
      createdOn: Date.now(),
      startDate: null,
      endDate: null,
    });

    return pickup;
  } catch (error) {
    return null;
  }
}
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:27,代码来源:create-new-pickup.ts

示例6: onChangeVolume

export default function onChangeVolume(
  app: ServerApp,
  connection: SocketConnection,
): SocketEventHandler<'user-settings:change-volume'> {
  const userSettings = app.service('user-settings');

  return async (data, cb) => {
    const user = connection.feathers.user;

    // Make sure a userId is authenticated
    if (!user) {
      return cb(new NotAuthenticated('You aren\'t authenticated!'));
    }

    if (data.volume < 0 || data.volume > 100) {
      return cb(new BadRequest('Not allowed volume value!'));
    }

    try {
      await userSettings.patch(user.id, { volume: data.volume });

      return cb(null);
    } catch (error) {
      log('Error while changing the volume', {
        userId: user.id,
        error,
        data,
      });

      return cb(error);
    }
  };
}
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:33,代码来源:on-change-volume.ts

示例7: onAcceptRules

export default function onAcceptRules(
  app: ServerApp,
  connection: SocketConnection,
): SocketEventHandler<'users:accept-rules'> {
  const users = app.service('users');

  return async (_, cb) => {
    const user = connection.feathers.user;

    // Make sure a userId is authenticated
    if (!user) {
      return cb(new NotAuthenticated('You aren\'t authenticated!'));
    }

    try {
      await users.patch(user.id, { hasAcceptedTheRules: true });

      log('User accepted the rules', { userId: user.id });

      return cb(null);
    } catch (error) {
      log('Error while accepting the rules', {
        userId: user.id,
        error,
      });

      return cb(error);
    }
  };
}
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:30,代码来源:on-accept-rules.ts

示例8: onChangeAnnouncer

export default function onChangeAnnouncer(
  app: ServerApp,
  connection: SocketConnection,
): SocketEventHandler<'user-settings:change-announcer'> {
  const userSettings = app.service('user-settings');

  return async (data, cb) => {
    const user = connection.feathers.user;

    // Make sure a userId is authenticated
    if (!user) {
      return cb(new NotAuthenticated('You aren\'t authenticated!'));
    }

    if (!(data.announcer in announcers)) {
      return cb(new BadRequest('Not a valid announcer!'));
    }

    try {
      await userSettings.patch(user.id, { announcer: data.announcer });

      return cb(null);
    } catch (error) {
      log('Error while changing the announcer', {
        userId: user.id,
        error,
        data,
      });

      return cb(error);
    }
  };
}
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:33,代码来源:on-change-announcer.ts

示例9:

      otherPlayers.map((player) => {
        if (player.map === pickup.map) {
          return app.service('players').patch(player.id, { map: null });
        }

        return player;
      })
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:7,代码来源:handle-create-pickup.ts

示例10: async

  return async (payload: Payload, meta: Meta) => {
    if (meta.provider === 'rest') {
      return;
    }

    try {
      const user = await getUserFromPayload(app, payload);

      if (user === null) {
        return;
      }

      // Check if the userId is still authenticated on any other socket
      const isStillConnected = app
        .channel('authenticated')
        .filter(connection => connection.user && connection.user.id === user.id)
        .length > 0;

      // Don't set the userId to online: false when the userId has still active sockets
      if (isStillConnected) {
        return;
      }

      await app.service('users').patch(user.id, {
        online: false,
        lastOnline: Date.now(),
      });

      log('Successfully logged out user', { userId: user.id });
    } catch (error) {
      log('Error in logout callback', { error });
    }
  };
开发者ID:TF2PickupNET,项目名称:TF2Pickup,代码行数:33,代码来源:on-logout.ts


注:本文中的@feathersjs/feathers.ServerApp.service方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。