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 (将#修改为@)