使用 React Native 和 Laravel 后端验证 Pusher

Jam*_*ons 5 javascript laravel pusher reactjs react-native

我正在尝试使用以下 React Native 脚本对私人广播进行身份验证。

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Pusher from 'pusher-js/react-native';

export default class App extends React.Component {
  componentWillMount() {
    Pusher.logToConsole = true;
    var pusher = new Pusher('*********', {
      authEndpoint: 'http://app.pgm/api/authtest',
      cluster: 'eu',
      encrypted: true
    });

    const channel = pusher.subscribe('private-chat-1');

  }
Run Code Online (Sandbox Code Playgroud)

上面的内容被发布到下面的函数中,下面的函数在从 Postman 测试时返回一个 auth 令牌。但是,当我通过 React Native 运行该应用程序时,我得到以下响应。

  public function pusher(Request $request){
        $pusher = new Pusher(config('broadcasting.connections.pusher.key'), config('broadcasting.connections.pusher.secret'), config('broadcasting.connections.pusher.app_id'));

        echo $pusher->socket_auth($request->channel_name, $request->socket_id);
  }
Run Code Online (Sandbox Code Playgroud)

[exp] Pusher : 无法检索身份验证信息。0客户端必须经过身份验证才能加入私有或在线通道。请参阅:https : //pusher.com/docs/authenticating_users [exp] Pusher:在私人聊天 1 上没有回调 pusher:subscription_error

这让我认为 Laravel 没有收到帖子数据。我目前没有任何可以阻止请求的中间件。

谁能看到我哪里出错了?

Ade*_*har 3

它在我这边的两端都工作得Postman很好React Native。我使用了下面的代码。就我而言,我没有使用 key encrypted: true

我正在成功监听事件。

代码

    // Pusher Logging
    Pusher.logToConsole = true;
    
    // Initialization & Configuration
    const pusher = new Pusher('****************', {
      cluster: '***',
      authEndpoint:
        'http://domain/products/chat/public/api/authtest',
    });

    // Making Connection
    pusher.connection.bind('connected', function (data) {
      console.log(data.socket_id);
    });

    // Subscribe Channel
    var channel = pusher.subscribe('private-channel-name', (data) => {
      console.log('Subscribe Channel');
      console.log(data);
    });

    // Accessing Channel
    const channelInfo = pusher.channel('private-chatify');
    console.log('channel Info');
    console.log(channelInfo);

    // Listen Event
    channel.bind('yourevent', function (data) {
      console.log('An event was triggered with message');
      console.log(data);
      console.log(data.message);
    });
Run Code Online (Sandbox Code Playgroud)

希望它可以帮助你。