使用React Native和Laravel后端进行Pusher身份验证

5

我正在尝试使用以下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');

  }

上述内容被发布到下面的函数中,当在Postman中测试时,该函数会返回一个身份验证令牌。但是,当我通过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);
  }

[exp] Pusher:无法检索身份验证信息。 0客户端必须经过身份验证才能加入私有或存在频道。请参阅:https://pusher.com/docs/authenticating_users [exp] Pusher:private-chat-1上没有回调函数,用于pusher:subscription_error

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

有人能看出我哪里出错了吗?


你找到解决方案了吗?我也遇到了同样的问题。 - Rajwant Kaur Boughan
1个回答

3

在我的Postman和React Native两端,它都正常工作。我使用了以下代码片段。在我的情况下,我没有使用密钥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);
    });

希望这能帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接