我一直有500错误,当我试图订阅一个私人渠道与推动者在我的反应原生应用程序。后端是laravel与护照oauth服务器。
我已经在推送器设置中设置了身份验证endpoint(http://url/broadcasting/auth)
反应本机推送装置
import Pusher from 'pusher-js/react-native';
Pusher.logToConsole = true;
var pusher = new Pusher('pusher-key', {
authEndpoint: 'http://url.com/broadcasting/auth',
cluster: 'ap1',
encrypted: true
});
var channel = pusher.subscribe('private-order');
channel.bind('App\\Events\\Order\\SiteHasAnsweredCheckIn', function(data) {
console.log('ok : ' + data);
});
和广播授权路由在我的路由/channel.php文件与回调简单地返回true,但我一直有一个500错误
路线/channels.php
Broadcast::channel('order', function () {
return true;
});
这是事件类
<?php
namespace App\Events\Order;
use App\Site;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class SiteHasAnsweredCheckIn implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $site;
public $user;
public $checkInConfirmed;
/**
* Create a new event instance.
*
* @param Site $site
* @param User $user
* @param $checkInConfirmed
*/
public function __construct(Site $site, User $user, $checkInConfirmed)
{
$this->site = $site;
$this->user = $user;
$this->checkInConfirmed = $checkInConfirmed;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('order');
}
}
来自控制台的反馈说:“Pusher:无法从您的webapp:500获取身份验证信息”对于删除Bugging来说并不是很有用。很难指出问题所在。你知道吗?
如果没有,我如何从推送器响应的超文本传输协议响应中获得更多信息/反馈?
非常感谢。
编辑:
在下面的答案之后,我更新了路线如下。请注意,api/*路由没有进行csrf令牌检查,因为我将它们从中间件中排除。
还是有500个错误。有没有办法了解Pusher抛出的错误的更多细节?如果有更多的反馈来了解正在发生的事情,那将是非常有用的。
新的身份验证endpoint:
Route::post('api/authtest', function() {
$pusher = new Pusher(config('broadcasting.connections.pusher.key'), config('broadcasting.connections.pusher.secret'), config('broadcasting.connections.pusher.app_id'));
echo $pusher->socket_auth($_POST[request()->channel_name], $_POST[request()->socket_id]);
})->middleware('auth:api');
编辑2:
我通过删除授权中间件使其正常工作。
Route::post('api/authtest', function() {
$pusher = new Pusher(config('broadcasting.connections.pusher.key'), config('broadcasting.connections.pusher.secret'), config('broadcasting.connections.pusher.app_id'));
echo $pusher->socket_auth($_POST[request()->channel_name], $_POST[request()->socket_id]);
});
但是很明显,这个想法是为了能够让用户提出这样的请求,这样我就可以给予适当的授权。
下面是我如何传递我的认证令牌到推送者调用从反应本地但我得到一个401响应。
let token = 'auth_access_token_taken_from_database';
var pusher = new Pusher('pasher_key', {
authEndpoint: 'http://url.com/api/authtest',
auth: {
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ' + token
}
},
cluster: 'ap1',
encrypted: true
});
var channel = pusher.subscribe('private-order');
使用上述中间件和令牌的所有其他oauth get和post请求都能在axios调用中正常工作。
有什么想法吗?
像这样编辑身份验证终结点
$user = auth()->user();
if ($user) {
$pusher = new Pusher('auth-key', 'secret', 'app_id');
$auth= $pusher->socket_auth(Input::get('channel_name'), Input::get('socket_id'));
$callback = str_replace('\\', '', $_GET['callback']);
header('Content-Type: application/javascript');
echo($callback . '(' . $auth . ');');
return;
}else {
header('', true, 403);
echo "Forbidden";
return;
}
身份验证endpoint不能简单地返回true
。它必须返回由服务器上的推送程序库生成的特殊令牌。客户端使用此令牌向推送器API演示允许连接到专用通道。查看此文档。