Laravel Echo and whisper

假如想象 提交于 2019-12-25 03:12:03

问题


I'm running echo server and redis. Private channels work perfectly, and messaging I have built for it works. Now I'm trying to get the whisper to work for the typing status as well but no luck. Does whisper require a pusher to work?

What I have tried on keyup (jquery)

Echo.private(chat- + userid)
.whisper('typing',{e: 'i am is typing...'});
console.log('key up'); // this one works so the keyup is triggered

then I'm of course listening the channel what I am whispering into:

Echo.private(chat- + userid).listenForWhisper('typing', (e) => {
console.log(e + ' this is typing');
});

But I get absolutely nothing anywhere. (debugging on at the echo server, nothing on console etc) Any help how to get this to work would be much appreciated.


回答1:


Your input event:

$('input').on('keydown', function(){
  let channel = Echo.private('chat')

  setTimeout( () => {
    channel.whisper('typing', {
      user: userid,
      typing: true
    })
  }, 300)
})

Your listening event:

Echo.private('chat')
  .listenForWhisper('typing', (e) => {
    e.typing ? $('.typing').show() : $('.typing').hide()
  })

setTimeout( () => {
  $('.typing').hide()
}, 1000)

Of course you have to have setup the authentication for this channel ahead of time to ensure that the trusted parties have access:

Broadcast::channel('chat', function ($user) {
    return Auth::check();
});

Where $user will be the userid we passed to the user param in our object on the front end.




回答2:


This is what my ReactJS componentDidMount looks like. Your listening event.

componentDidMount() {
let timer; // timer variable to be cleared every time the user whispers

Echo.join('chatroom')
  .here(...)
  .joining(...)
  .leaving(...)
  .listen(...)
}).listenForWhisper('typing', (e) => {

  this.setState({
    typing: e.name
  });

  clearTimeout(timer); // <-- clear
  // Take note of the 'clearTimeout' before setting another 'setTimeout' timer.
  // This will clear previous timer that will make your typing status
  // 'blink' if not cleared.
  timer = setTimeout(() => {
    this.setState({
      typing: null
    });
  }, 500);

});
}


来源:https://stackoverflow.com/questions/51524551/laravel-echo-and-whisper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!