【问题标题】:Authenticating Pusher using React Native and a Laravel Backend使用 React Native 和 Laravel 后端对 Pusher 进行身份验证
【发布时间】:2018-08-09 22:48:34
【问题描述】:

我正在尝试使用以下 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 没有收到帖子数据。我目前没有任何可以阻止请求的中间件。

谁能看出我哪里出错了?

【问题讨论】:

  • 你找到解决办法了吗,我也遇到同样的问题

标签: javascript laravel reactjs react-native pusher


【解决方案1】:

我在PostmanReact 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);
    });

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 2020-12-20
    • 2023-02-14
    相关资源
    最近更新 更多