问题描述
我正在使用
拉威尔:8.54 拉威尔·圣殿:2.11 Pusher PHP服务器:7.0 NuxtJs:2.15.7 Pusher-js:7.0.4 LAVELEL-ECHO:1.11.3事件类
<?php
namespace AppEvents;
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcastNow;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
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 IlluminateBroadcastingChannel|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);
});
我可以看到广播消息进入Pusher仪表板,也可以看到客户端连接,用户可以完美地进行身份验证,问题是在身份验证后没有订阅。请在订阅前查看这些用户身份验证请求的屏幕截图。你知道我该怎么解决这个问题吗?
推荐答案
好的解决了这个问题,在我的nuxt插件文件中,您只需返回response
var Notrespose.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)
}