簡體   English   中英

在php中為推桿創建私有通道身份驗證流

[英]create private channel auth flow in php for 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);

在推入器API的php和更高版本的js中驗證用戶的正確方法是什么?

您將需要創建另一個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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM