监听websocket事件laravel echo

68de4m5k  于 2021-06-09  发布在  Redis
关注(0)|答案(0)|浏览(321)

echo、socket.io、redis和我的实时私人聊天正常工作,但我想在用户键入或用户加入聊天室或用户离开聊天室时收听socket事件。
该类是userevent类

class UserEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public $user;
public $message;
/**
 * Create a new event instance.
 *
 * @param User $user
 */
public function __construct(User $user, $message)
{
    $this->user = $user;
    $this->message = $message;
}

/**
 * Get the channels the event should broadcast on.
 *
 * @return \Illuminate\Broadcasting\Channel|array
 */
public function broadcastOn()
{
    return new PrivateChannel('user.' . $this->user->id);
}

public function broadcastAs()
{
    return 'UserEvent';
}

public function broadcastWith()
{
    return [
        'user'=>$this->user,
        'message'=>$this->message
    ];
}

}
这就是userchannel中的join方法

public function join(User $user, User $userClient)
{
    return $user->id === $userClient->id;
}

这是监听器优先。userevent监听器工作,我得到实时消息,但ListenForWisper不工作

this.channel
        .listen('.UserEvent', data => {
            this.msgs.push({text:data.message,main:false})
        })
        .listenForWhisper('typing', (e) => {
            console.log('this is event ', e);
            if(e.typing){
                console.log(e.typing)
            }
        });

另一个细节
我有按键监听

keymonitor(event){
            this.channel
                .whisper('typing', {
                    typing: true,
                });
            console.log('typing')
        },

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题