在 php 中为推送器创建私人频道身份验证流程
create private channel auth flow in php for pusher
我想使用 pusher PHP/JS API 实现私人消息聊天。
我需要一些帮助来使用 php 设置私人频道和身份验证端点,文档中不清楚是否有默认方法来管理私人频道的用户身份验证。我用谷歌搜索了一下,找到了一些示例,但我没有使用 laravel,所以我无法应用它们。任何建议将不胜感激。
JS
Pusher.logToConsole = true;
var pusher = new Pusher('12xxxxxx', {
cluster: 'us',
forceTLS: true
});
var channel = pusher.subscribe('private-encrypted-test-channel');
channel.bind('message-event', function(data) {
alert(JSON.stringify(data));
});
channel.bind('pusher:subscription_succeeded', function(members) {
console.log(members);
console.log('successfully subscribed!');
});
PHP
require_once __DIR__.'/vendor/autoload.php';
$options = array(
'cluster' => 'eu',
'useTLS' => true
);
$pusher = new Pusher\Pusher(
'12xxxxx',
'2xxxxxx',
'8xxxxxx',
$options
);
$data['message'] = 'hello world';
$pusher->trigger('private-encrypted-test-channel', 'message-event', $data);
在 php 中以及之后在 js 中为推送器 API 验证用户的正确方法是什么?
您将需要创建另一个 PHP 文件(例如 pusher_auth.php)来执行身份验证。使用此代码作为起点(改编自您的代码和 Pusher 的 WordPress 文档,因为 WordPress 的底层只是 PHP 层):
require_once __DIR__.'/vendor/autoload.php';
$options = array(
'cluster' => 'eu',
'useTLS' => true
);
$pusher = new Pusher\Pusher(
'12xxxxx',
'2xxxxxx',
'8xxxxxx',
$options
);
// You need to define this function for your application, but for testing purposes, it always returns true
function user_is_authenticated(){
// Insert your logic here
return true;
}
if ( user_is_authenticated() )
{
echo $pusher->socket_auth($_POST['channel_name'], $_POST['socket_id']);
}
else
{
header('', true, 403);
echo "Forbidden";
}
现在像这样修改您的 JS 代码以添加 authEndpoint 参数(更改名称和相对路径以匹配您的 PHP 身份验证文件)
var pusher = new Pusher('12xxxxxx', {
cluster: 'us',
authEndpoint: '/pusher_auth.php',
forceTLS: true
});
我想使用 pusher PHP/JS API 实现私人消息聊天。 我需要一些帮助来使用 php 设置私人频道和身份验证端点,文档中不清楚是否有默认方法来管理私人频道的用户身份验证。我用谷歌搜索了一下,找到了一些示例,但我没有使用 laravel,所以我无法应用它们。任何建议将不胜感激。
JS
Pusher.logToConsole = true;
var pusher = new Pusher('12xxxxxx', {
cluster: 'us',
forceTLS: true
});
var channel = pusher.subscribe('private-encrypted-test-channel');
channel.bind('message-event', function(data) {
alert(JSON.stringify(data));
});
channel.bind('pusher:subscription_succeeded', function(members) {
console.log(members);
console.log('successfully subscribed!');
});
PHP
require_once __DIR__.'/vendor/autoload.php';
$options = array(
'cluster' => 'eu',
'useTLS' => true
);
$pusher = new Pusher\Pusher(
'12xxxxx',
'2xxxxxx',
'8xxxxxx',
$options
);
$data['message'] = 'hello world';
$pusher->trigger('private-encrypted-test-channel', 'message-event', $data);
在 php 中以及之后在 js 中为推送器 API 验证用户的正确方法是什么?
您将需要创建另一个 PHP 文件(例如 pusher_auth.php)来执行身份验证。使用此代码作为起点(改编自您的代码和 Pusher 的 WordPress 文档,因为 WordPress 的底层只是 PHP 层):
require_once __DIR__.'/vendor/autoload.php';
$options = array(
'cluster' => 'eu',
'useTLS' => true
);
$pusher = new Pusher\Pusher(
'12xxxxx',
'2xxxxxx',
'8xxxxxx',
$options
);
// You need to define this function for your application, but for testing purposes, it always returns true
function user_is_authenticated(){
// Insert your logic here
return true;
}
if ( user_is_authenticated() )
{
echo $pusher->socket_auth($_POST['channel_name'], $_POST['socket_id']);
}
else
{
header('', true, 403);
echo "Forbidden";
}
现在像这样修改您的 JS 代码以添加 authEndpoint 参数(更改名称和相对路径以匹配您的 PHP 身份验证文件)
var pusher = new Pusher('12xxxxxx', {
cluster: 'us',
authEndpoint: '/pusher_auth.php',
forceTLS: true
});