简体   繁体   English

Laravel echo 收听已读消息

[英]Laravel echo listen readed message

How I can listen, if message seen by user?如果用户看到消息,我如何收听?

I have code:我有代码:

 watch: { message() { Echo.private('chat') .whisper('typing', { name: this.message }); } }, methods: { send(){ if(this.message.length != 0 && this.message.length <= 4000) { this.chat.message.push(this.message); this.chat.user.push('you'); this.chat.time.push(this.getTime()); axios.post('/sendMessage', { message: this.message, //lastName: 'Flintstone' }) .then(response => { console.log(response); this.message = ''; }) .catch(error => { console.log(error); }); } }, getTime() { let time = new Date(); return time.getHours() + ':' + time.getMinutes(); } }, mounted() { Echo.private('chat') .listen('ChatEvent', (e) => { this.chat.message.push(e.message); this.chat.user.push(e.user); this.chat.time.push(this.getTime()); console.log(e); }) .listenForWhisper('typing', (e) => { if(e.name != '') this.typing = 'typing..'; else this.typing = null; }); }

I updated:我更新了:

 methods: { seenMessage() { axios.post('/setMessagesSeen/' + this.convId) .then( response => { this.chat.seen = ''; }) .catch( response => { console.log(response) } ) }, send(){ if(this.message.length != 0 && this.message.length <= 4000) { this.chat.message.push(this.message); this.chat.user.push(this.user); this.chat.time.push(this.getTime()); this.chat.seen.push('unread'); axios.post('/sendMessage/' + this.convId, { message: this.message, }) .then(response => { console.log(response); this.message = ''; }) .catch(error => { console.log(error); }); } }, getTime() { let time = new Date(); return time.getHours() + ':' + time.getMinutes(); } },

How I can send class "unread" to element div other user?如何将“未读”类发送到元素 div 其他用户? I can paste class on current user, and I get color on element chat only for me, but how I can hide element for me and other user, when message is seen?我可以在当前用户上粘贴类,并且我只为我获取元素聊天的颜色,但是当看到消息时,我如何为我和其他用户隐藏元素?


I can watch, if message typing.如果输入消息,我可以观看。 But How I can listen if message seen by user?但是,如果用户看到消息,我如何收听? I need do actions, if message sended for seen message or how?我需要采取行动,如果消息发送看到的消息或如何?

To detect if user clicked to textarea, try this :要检测用户是否单击了 textarea,请尝试以下操作:

  • First, create a method hasSeen in your Vuejs Instance.首先,在你的 Vuejs 实例中创建一个 hasSeen 方法。

    \n ..., ...,\n methods : {方法 : {\n   send : () => {...},发送 : () => {...},\n   getTime: () => {...}, getTime: () => {...},\n   hasSeen: () => {已经看到:() => { \n            axios.put('your-api-or-backend-domains/setHasSeenMessage') axios.put('your-api-or-backend-domains/setHasSeenMessage')\n                 .then( response => { //Okay }) .then( response => { //好的 })\n                 .catch(response => { console.log(response)}) .catch(response => { console.log(response)})\n           } }\n  } }    \n
    • Then, in your textarea just add onclickEvent:然后,在您的 textarea 中添加 onclickEvent:

    <textarea> name="message" @click="hasSeen"></textarea>

If you want to do things like facebook I found a logic here : https://www.quora.com/How-does-Facebook-know-if-a-chat-message-is-seen如果你想做facebook 之类的事情,我在这里找到了一个逻辑: https : //www.quora.com/How-does-Facebook-know-if-a-chat-message-is-seen

  • Case 1: The Facebook tab is in focus but the chat-reply text field is not.案例 1:Facebook 选项卡是焦点,但聊天回复文本字段不是。

It sends out a "seen" notification (ajaxRequest) when you click on the field to type, using the focus event listener associated with the text-field.当您单击要键入的字段时,它会使用与文本字段关联的焦点事件侦听器发送“已看到”通知 (ajaxRequest)。

  • Case 2: The Facebook tab is not in focus案例 2:Facebook 选项卡未聚焦

  • In case you already had the text box in focus, but were on a different tab, the notification(AjaxRequest) is sent when you switch to the facebook tab.如果您已经将文本框置于焦点上,但位于不同的选项卡上,则当您切换到 facebook 选项卡时会发送通知(AjaxRequest)。 The JavaScript Window Focus and Window Blur events can be used to detect if the tab is in focus. JavaScript Window Focus 和 Window Blur 事件可用于检测选项卡是否处于焦点。 These and the onfocus event for the textbox can be used to send out the notification(AjaxRequest).这些和文本框的 onfocus 事件可用于发送通知(AjaxRequest)。

So you can create a javascript script that can implement this logic!所以你可以创建一个可以实现这个逻辑的javascript脚本!

I hope it helped you!我希望它对你有帮助!

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

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