监听Websocket事件Laravel-Echo

时间:2020-05-31 09:43:32

标签: laravel redis socket.io laravel-echo

echo,socket.io,redis和我的实时私人聊天正常运行,但是我想在用户键入内容或何时加入房间或离开房间时监听套接字事件。

该类为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中的加入方法

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

那是听众 首先。UserEvent侦听器正常工作,但我收到实时消息,但listenForWhisper无法正常工作

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)
            }
        });

其他细节

我有keydown侦听器

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

0 个答案:

没有答案