繁体   English   中英

Laravel回声和耳语

[英]Laravel Echo and whisper

我正在运行echo服务器和redis。 私人频道可完美运作,而我为此建立的讯息则可正常运作。 现在,我正在尝试使耳语也可以用于打字状态,但是没有运气。 耳语是否需要推动器才能起作用?

我在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

然后我当然会在频道中窃听我的声音:

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

但是我什么都没得到。 (在回显服务器上调试,在控制台上什么也没有,等等)如何使它工作的任何帮助将不胜感激。

您的输入事件:

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

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

您的聆听事件:

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

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

当然,您必须提前为此通道设置身份验证,以确保受信方可以访问:

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

其中$user将是我们传递给前端对象中user参数的user userid

这就是我的ReactJS componentDidMount的样子。 您的聆听事件。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM