我正在开发一个具有React前端的Laravel应用程序。我有使用pusher进行的通知设置,都可以正常工作。
我正在尝试使用https://pusher.com/docs/channels/using_channels/encrypted-channels中所述的端到端加密
在我的config / broadcasting.php中,我有以下内容
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => true,
'encryption_master_key' => env('PUSHER_ENCRYPTION_KEY'),
],
]
在我的.env
中设置了PUSHER_ENCRYPTION_KEY
,我可以在Pusher调试控制台中看到通过加密的通知,如我所愿。
在前端,我像这样实例化Pusher
const socket = new Pusher(process.env.MIX_PUSHER_APP_KEY, {
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
authEndpoint: '/api/broadcasting/auth'
});
socket.subscribe('private-encrypted-user.' + props.user.id);
socket.bind('App\\Events\\TaskCreated' , function (data) {
console.log(data)
});
我可以在我的网络标签中看到/api/broadcasting/auth
带有一个额外字段,现在称为shared_secret
。根据文档,这用于解密传入的通知,并且解密应该是自动的。当我触发App\\Events\\TaskCreated
console.log(data)
运行但数据仍被加密时。
它在文档if a client is unable to decrypt a message, it will make another request to your auth endpoint to obtain a new decryption key. If the client is still unable to decrypt the message with the new key, it will throw an error.
中也说过,但没有引发任何错误,也没有对/api/broadcasting/auth
发出进一步的请求
我已启用Pusher.logToConsole = true;
,一切看起来都很好。我什至得到"pusher_internal:subscription_succeeded","channel":"private-encrypted-user.2"
,这使我相信事情的auth方面正在按预期工作。
谁能看到我想念的东西吗?
我解决了!
在上面的代码中,当我需要将事件绑定到通道时,我将事件绑定到套接字。浪费了一天,但是下面的代码可以正常工作,以防其他人遇到同样的问题
const socket = new Pusher(process.env.MIX_PUSHER_APP_KEY, {
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
authEndpoint: '/api/broadcasting/auth'
});
const channel = socket.subscribe('private-encrypted-user.' + props.user.id);
channel.bind('App\\Events\\TaskCreated' , function (data) {
console.log(data)
});