如何解决Laravel Livewire Echo对接会
我正在尝试创建一个配对系统。找到匹配项后,我想向找到该匹配项的用户广播。我无法弄清楚-应该改用轮询吗?
“我的用户”模型与“匹配”模型有一个belongsToMany关系。
这里的代码无效。
路线:
Broadcast::channel('match.{matchId}',function ($user,$matchId) {
return $user->matches()->contains('id',$matchId);
});
组件:
class PlayComponent extends Component
{
public $matchId;
public function getListeners()
{
return [
"echo-private:match.{$this->matchId},JoinMatch" => 'joinMatch',];
}
public function render()
{
return view('livewire.play-component',[
'matches' => Auth::user()->matches()->get()->toArray(),]);
}
public function play()
{
if ($match = Match::where('status','waiting')->first()) {
$this->matchId = $match->id;
event(new JoinMatch($match));
}
else {
$this->matchId = Auth::user()->matches()->create(['status' => 'waiting']);
}
}
public function joinMatch()
{
// ???
}
}
查看:
<div>
<button class="btn btn-success" wire:click="play">Play</button>
<hr>
<pre>{{ print_r($matches,true) }}</pre>
</div>
事件:
class JoinMatch implements ShouldBroadcast
{
use Dispatchable,InteractsWithSockets,SerializesModels;
public $match;
public function __construct(Match $match)
{
$this->match = $match;
$this->match->users()->attach(Auth::user()->id);
}
public function broadcastOn()
{
return new PrivateChannel('match.' . $this->match->id);
}
}
我如何进行这项工作?我认为问题是getListeners
需要一个参数,但该参数对于匹配的用户可能尚不存在。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。