0

プッシャー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 を使用する正しい方法は何ですか?

4

1 に答える 1

1

認証を実行するには、別の PHP ファイル (pusher_auth.php など) を作成する必要があります。このコードを出発点として使用します (フードの下の WordPress は PHP の層にすぎないため、コードと Pusher の WordPress ドキュメントから適応されます)。

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
});
于 2019-09-29T01:51:55.547 に答える