laravel 即使用户已通过身份验证,echo 也不会订阅私人频道
laravel echo wont subscribe to private channel even the user is authenticated
我正在使用
- Laravel: 8.54
- Laravel Sanctum: 2.11
- 推送器Php 服务器:7.0
- NuxtJs:2.15.7
- pusher-js: 7.0.4
- laravel-echo: 1.11.3
事件Class
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class RaffleEntriesCreated implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $userid;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($userid)
{
$this->userid = $userid;
}
/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'private-raffle-entry';
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('RaffleEntry.'.$this->userid);
}
}
在我的频道文件中
Broadcast::channel('RaffleEntry.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
在 nuxt 插件上
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
export default ({ app }, inject) => {
const echo = new Echo({
broadcaster: 'pusher',
key: "mykey",
authEndpoint: `http://back.project888.test/api/broadcasting/auth`,
encrypted: true,
forceTLS: true,
cluster: 'ap2',
authorizer: (channel, options) => {
return {
authorize: (socketId, callback) => {
app.$axios.$post('/api/broadcasting/auth', {
socket_id: socketId,
channel_name: channel.name
})
.then(response => {
callback(false, response.data);
console.log('from oklugib ');
})
.catch(error => {
callback(true, error);
});
}
};
}
});
inject(
'echo', echo)
}
客户代码
this.$echo.private(`RaffleEntry.${this.$auth.user.id}`)
.listen('.private-raffle-entry', (e) => {
console.log(e);
});
我可以在推送器仪表板和客户端连接中看到广播消息,用户可以完美地进行身份验证,问题是身份验证后它没有订阅。请在订阅前查看这些用户身份验证请求的屏幕截图。你知道我该如何解决这个问题吗?
好的,这个问题已经解决了,在我的 nuxt 插件文件上,授权选项你只是 return response
var 而不是 respose.data
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
export default ({ app }, inject) => {
const echo = new Echo({
broadcaster: 'pusher',
key: "mykey",
authEndpoint: `http://back.project888.test/api/broadcasting/auth`,
encrypted: true,
forceTLS: true,
cluster: 'ap2',
authorizer: (channel, options) => {
return {
authorize: (socketId, callback) => {
app.$axios.$post('/api/broadcasting/auth', {
socket_id: socketId,
channel_name: channel.name
})
.then(response => {
callback(false, response);
})
.catch(error => {
callback(true, error);
});
}
};
}
});
inject(
'echo', echo)
}
我正在使用
- Laravel: 8.54
- Laravel Sanctum: 2.11
- 推送器Php 服务器:7.0
- NuxtJs:2.15.7
- pusher-js: 7.0.4
- laravel-echo: 1.11.3
事件Class
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class RaffleEntriesCreated implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $userid;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($userid)
{
$this->userid = $userid;
}
/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'private-raffle-entry';
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('RaffleEntry.'.$this->userid);
}
}
在我的频道文件中
Broadcast::channel('RaffleEntry.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
在 nuxt 插件上
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
export default ({ app }, inject) => {
const echo = new Echo({
broadcaster: 'pusher',
key: "mykey",
authEndpoint: `http://back.project888.test/api/broadcasting/auth`,
encrypted: true,
forceTLS: true,
cluster: 'ap2',
authorizer: (channel, options) => {
return {
authorize: (socketId, callback) => {
app.$axios.$post('/api/broadcasting/auth', {
socket_id: socketId,
channel_name: channel.name
})
.then(response => {
callback(false, response.data);
console.log('from oklugib ');
})
.catch(error => {
callback(true, error);
});
}
};
}
});
inject(
'echo', echo)
}
客户代码
this.$echo.private(`RaffleEntry.${this.$auth.user.id}`)
.listen('.private-raffle-entry', (e) => {
console.log(e);
});
我可以在推送器仪表板和客户端连接中看到广播消息,用户可以完美地进行身份验证,问题是身份验证后它没有订阅。请在订阅前查看这些用户身份验证请求的屏幕截图。你知道我该如何解决这个问题吗?
好的,这个问题已经解决了,在我的 nuxt 插件文件上,授权选项你只是 return response
var 而不是 respose.data
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
export default ({ app }, inject) => {
const echo = new Echo({
broadcaster: 'pusher',
key: "mykey",
authEndpoint: `http://back.project888.test/api/broadcasting/auth`,
encrypted: true,
forceTLS: true,
cluster: 'ap2',
authorizer: (channel, options) => {
return {
authorize: (socketId, callback) => {
app.$axios.$post('/api/broadcasting/auth', {
socket_id: socketId,
channel_name: channel.name
})
.then(response => {
callback(false, response);
})
.catch(error => {
callback(true, error);
});
}
};
}
});
inject(
'echo', echo)
}