本文整理匯總了TypeScript中@hapi/hapi.Server.subscription方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Server.subscription方法的具體用法?TypeScript Server.subscription怎麽用?TypeScript Server.subscription使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@hapi/hapi.Server
的用法示例。
在下文中一共展示了Server.subscription方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
server.register(Nes).then(() => {
server.subscription('/item/{id}');
return server.start().then(() => {
server.publish('/item/5', {id: 5, status: 'complete'});
server.publish('/item/6', {id: 6, status: 'initial'});
});
})
示例2: async
server.register([Basic, Nes]).then(() => {
// Set up HTTP Basic authentication
interface User {
username: string;
password: string;
name: string;
id: string;
}
const users: {[index: string]: User} = {
john: {
username: 'john',
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
name: 'John Doe',
id: '2133d32a'
}
};
const validate: Basic.Validate = async (request, username, password, h) => {
const user = users[username];
if (!user) {
return { credentials: null, isValid: false };
}
let isValid = await Bcrypt.compare(password, user.password)
return { isValid, credentials: { id: user.id, name: user.name, username: user.username } };
};
server.auth.strategy('simple', 'basic', { validate });
server.auth.default('simple')
// Set up subscription
server.subscription('/items', {
filter: (path, message, options) => {
return message.updater !== options.credentials.username;
}
});
server.start().then(() => {
server.publish('/items', { id: 5, status: 'complete', updater: 'john' });
server.publish('/items', { id: 6, status: 'initial', updater: 'steve' });
});
});