[英]Authenticating Pusher using React Native and a Laravel Backend
我正在尝试使用以下 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:pusher:subscription_error 在 private-chat-1 上没有回调
这让我认为 Laravel 没有收到发布数据。 我目前没有任何可以阻止请求的中间件。
谁能看到我哪里出错了?
它在我这边在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);
});
希望它可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.