我在Vercel上使用next.js托管应用程序。为了在应用程序中向用户提供实时消息,我想实现Pusher。每个用户都应该通过一个通道访问,当然是私有和安全的。
根据日志,一切似乎都在工作,但是,我不能管理订阅频道和获得事件的消息。我已经测试了几种可能性,然而,到目前为止,我还没有成功。
- 服务器端:/pusher/user-auth/index. ts**
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const pusherService = container.resolve(PusherService);
const manageToken = container.resolve(ManageToken);
const loggingService = container.resolve(LoggingService);
loggingService.info('PUSHER /api/pusher/user-auth - Authenticate user');
const socketId = req.body.socket_id as string;
const token = req.headers['x-csrf-token'] as string;
const currentUser = await manageToken.verifyUserToken(token);
const authResponse = await pusherService.authenticateUser(socketId, {
id: currentUser.id,
user_info: {
lastName: currentUser.lastName,
firstName: currentUser.firstName,
email: currentUser.email,
},
});
res.status(200).json(authResponse);
return;
}
- 服务器端:/pusher/auth/index.ts**
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const pusherService = container.resolve(PusherService);
const loggingService = container.resolve(LoggingService);
loggingService.info('PUSHER /api/pusher/auth - Authorize channel.');
const socketId = req.body.socket_id as string;
const channel = req.body.channel_name;
const authResponse = pusherService.authorizeChannel(socketId, channel);
res.status(200).json(authResponse);
return;
}
- 服务器端:/pusher-service. ts**
public async sendToUser(userId: string, event: string, data: any) {
this.loggingService.info('Send event to user via Pusher.');
try {
return this.pusher.sendToUser(userId, 'generated', {message: 'hi'});
} catch (e) {
this.loggingService.error(e);
}
}
客户端**
export const subscribe = async (token) => {
log.info("Subscribe pusher channel.");
Pusher.logToConsole = true;
const pusher = new Pusher(process.env.REACT_APP_PUSHER_KEY, {
cluster: "eu",
channelAuthorization: {
endpoint: process.env.REACT_APP_BLOOO_API_URL + "/pusher/auth"
},
userAuthentication: {
endpoint: process.env.REACT_APP_BLOOO_API_URL + "/pusher/user-auth",
headers: {
"X-CSRF-Token": "Bearer " + token
}
}
});
pusher.signin();
// SUBSCRIBE
// e.g. does not work
// const channel = pusher.subscribe("#server-to-user-444")
// channel.bind("generated", (data) => {
// console.log(data);
// });
};
- 客户端:日志**
Pusher : : ["State changed","initialized -> connecting"]
logger.ts:19 Pusher : : ["Connecting",{"transport":"ws","url":"wss://ws-eu.pusher.com:443/app/xxx?protocol=7&client=js&version=8.1.0&flash=false"}]
logger.ts:19 Pusher : : ["State changed","connecting -> connected with new socket ID 123.123"]
logger.ts:19 Pusher : : ["Event sent",{"event":"pusher:signin","data":{"auth":"123:123","user_data":"{\"id\":\"444\",\"user_info\":{\"lastName\":\"NAME\",\"firstName\":\"NAME\",\"email\":\"MAIL\"}}"}}]
logger.ts:19 Pusher : : ["Event recd",{"event":"pusher:signin_success","data":{"user_data":"{\"id\":\"444\",\"user_info\":{\"lastName\":\"NAME\",\"firstName\":\"NAME\",\"email\":\"MAIL\"}}"}}]
logger.ts:19 Pusher : : ["Event sent",{"event":"pusher:subscribe","data":{"auth":"","channel":"#server-to-user-444"}}]
logger.ts:19 Pusher : : ["Event recd",{"event":"pusher_internal:subscription_succeeded","channel":"#server-to-user-444","data":{}}]
logger.ts:19 Pusher : : ["No callbacks on #server-to-user-444 for pusher:subscription_succeeded"]
logger.ts:19 Pusher : : ["Event recd",{"event":"generated","channel":"#server-to-user-444","data":{"message":"hi"}}]
logger.ts:19 Pusher : : ["No callbacks on user for generated"]
logger.ts:19 Pusher : : ["No callbacks on #server-to-user-444 for generated"]
1条答案
按热度按时间dxpyg8gm1#
好的,我找到了一个绑定到用户而不是频道的解决方案。
https://pusher.com/docs/channels/using_channels/events/#binding-on-the-user-object