简体   繁体   English

使用 React Native 和 Laravel 后端验证 Pusher

[英]Authenticating Pusher using React Native and a Laravel Backend

Im trying to authenticate a private broadcast using the following React Native script.我正在尝试使用以下 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');

  }

The above is being posted to the function below, the function below returns an auth token when tested from Postman.上面的内容被发布到下面的函数,当从 Postman 测试时,下面的函数返回一个授权令牌。 However when I run the app through React Native I get the following response.但是,当我通过 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: Couldn't retrieve authentication info. [exp] Pusher:无法检索身份验证信息。 0Clients must be authenticated to join private or presence channels. 0客户端必须经过身份验证才能加入私人或在线频道。 See: https://pusher.com/docs/authenticating_users [exp] Pusher: No callbacks on private-chat-1 for pusher:subscription_error请参阅: https ://pusher.com/docs/authenticating_users [exp] Pusher:pusher:subscription_error 在 private-chat-1 上没有回调

It leads me to think Laravel isn't receiving the post data.这让我认为 Laravel 没有收到发布数据。 I do not currently have any middleware that could block the request.我目前没有任何可以阻止请求的中间件。

Can anyone see where I am going wrong?谁能看到我哪里出错了?

It's working fine on my side on both Postman and React Native ends.它在我这边在PostmanReact Native端都运行良好。 I used following piece of code.我使用了以下代码。 In my case I'm not using key encrypted: true .在我的例子中,我没有使用 key encrypted: true

I'm listening event successfully.我正在成功收听事件。

Code代码

    // 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);
    });

Hope it may help you.希望它可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM