Laravel Echo - 无法加入状态频道
Laravel Echo - can't join presence channel
我正在尝试使用 Javascript 的库 Echo 加入 Presence 频道。我可以成功加入常规频道了
所以,这就是我所做的:
我这样触发事件:
event(new FixtureChatroomEvent($userId, $message, $username, $profileImg, $fixtureId));
或者像这样
broadcast(new FixtureChatroomEvent($userId, $message, $username, $profileImg, $fixtureId))->toOthers();
事件是这样的(看起来很难看,我知道,稍后我会传一个class):
class FixtureChatroomEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public $username;
public $profileImg;
public $userId;
public $fixtureId;
public function __construct($userId, $message, $username, $profileImg, $fixtureId)
{
$this->message = $message;
$this->username = $username;
$this->profileImg = $profileImg;
$this->userId = $userId;
$this->fixtureId = $fixtureId;
}
public function broadcastOn()
{
return new PresenceChannel('fixture-channel.' . $this->fixtureId);
}
}
listener 方法很简单,我只用 handle 方法,如下所示:
public function handle(FixtureChatroomEvent $event)
{
return $event;
}
在EventServiceProvider
中也是这样注册的:
'App\Events\FixtureChatroomEvent' => [
'App\Listeners\FixtureChatroomEventListener'
],
channels.php(路线)看起来像这样:
Broadcast::channel('fixture-channel.{fixtureId}', function ($user, $message, $username, $profileImg, $userId, $fixtureId) {
return [
'message' => $message,
'username' => $username,
'profileImg' => $profileImg,
'userId' => $userId,
'fixtureId' => $fixtureId
];
});
而 BroadcastServiceProvider
看起来像这样:
public function boot()
{
Broadcast::routes(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
}
现在前端:我只是尝试做这样的事情(我将灯具 ID 传递给与事件相同的频道名称部分 fixtureId
- 频道名称是正确的输出):
$(document).ready(function(){
var fixtureChannel =`fixture-channel.${$('#fixture_id').text()}`;
//var fixtureChannel = 'fixture-channel';
console.log(fixtureChannel);
Echo.join(fixtureChannel)
.listen('FixtureChatroomEvent', (e) => {
alert('ok');
console.log('ok');
})
.here((users) => {
console.log('123');
})
.joining((user) => {
console.log(user.name);
})
.leaving((user) => {
console.log(user.name);
})
;
});
现在事件被触发了,但是另一边什么也看不到。我也尝试加入没有 fixtureId 的存在频道,但没有成功。没有显示错误。我怎么调试这个?
包调试器 (laravel-websockets) 显示事件已触发,但未看到任何连接:
问题出在 Broadcast::routes(['middleware' => ['auth:api']]);
行。我删除了中间件,如下所示: Broadcast::routes();
我在发布此问题之前尝试过此操作,但这是第一次,服务器 returns 出现 500 错误。只需再次刷新页面即可。
我正在尝试使用 Javascript 的库 Echo 加入 Presence 频道。我可以成功加入常规频道了
所以,这就是我所做的:
我这样触发事件:
event(new FixtureChatroomEvent($userId, $message, $username, $profileImg, $fixtureId));
或者像这样
broadcast(new FixtureChatroomEvent($userId, $message, $username, $profileImg, $fixtureId))->toOthers();
事件是这样的(看起来很难看,我知道,稍后我会传一个class):
class FixtureChatroomEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public $username;
public $profileImg;
public $userId;
public $fixtureId;
public function __construct($userId, $message, $username, $profileImg, $fixtureId)
{
$this->message = $message;
$this->username = $username;
$this->profileImg = $profileImg;
$this->userId = $userId;
$this->fixtureId = $fixtureId;
}
public function broadcastOn()
{
return new PresenceChannel('fixture-channel.' . $this->fixtureId);
}
}
listener 方法很简单,我只用 handle 方法,如下所示:
public function handle(FixtureChatroomEvent $event)
{
return $event;
}
在EventServiceProvider
中也是这样注册的:
'App\Events\FixtureChatroomEvent' => [
'App\Listeners\FixtureChatroomEventListener'
],
channels.php(路线)看起来像这样:
Broadcast::channel('fixture-channel.{fixtureId}', function ($user, $message, $username, $profileImg, $userId, $fixtureId) {
return [
'message' => $message,
'username' => $username,
'profileImg' => $profileImg,
'userId' => $userId,
'fixtureId' => $fixtureId
];
});
而 BroadcastServiceProvider
看起来像这样:
public function boot()
{
Broadcast::routes(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
}
现在前端:我只是尝试做这样的事情(我将灯具 ID 传递给与事件相同的频道名称部分 fixtureId
- 频道名称是正确的输出):
$(document).ready(function(){
var fixtureChannel =`fixture-channel.${$('#fixture_id').text()}`;
//var fixtureChannel = 'fixture-channel';
console.log(fixtureChannel);
Echo.join(fixtureChannel)
.listen('FixtureChatroomEvent', (e) => {
alert('ok');
console.log('ok');
})
.here((users) => {
console.log('123');
})
.joining((user) => {
console.log(user.name);
})
.leaving((user) => {
console.log(user.name);
})
;
});
现在事件被触发了,但是另一边什么也看不到。我也尝试加入没有 fixtureId 的存在频道,但没有成功。没有显示错误。我怎么调试这个?
包调试器 (laravel-websockets) 显示事件已触发,但未看到任何连接:
问题出在 Broadcast::routes(['middleware' => ['auth:api']]);
行。我删除了中间件,如下所示: Broadcast::routes();
我在发布此问题之前尝试过此操作,但这是第一次,服务器 returns 出现 500 错误。只需再次刷新页面即可。