當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。