【问题标题】:How to use pusher with laravel and vue js to create real time chat?如何使用带有 laravel 和 vue js 的推送器来创建实时聊天?
【发布时间】:2019-11-25 12:15:06
【问题描述】:

我是 laravel 的新手,我与 laravel 和 vue js 以及 pusher 进行了实时聊天。我想代码一切都很好,但聊天不是实时的。我需要帮助,只是想知道我的代码哪里错了。下面是我的代码。

<?php

namespace App\Events;

use App\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

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

    public $message;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(Message $message)
    {
        //
         $this->message = $message;
    }

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

    public function broadcastWith()
    {
        $this->message->load('fromContact');

        return ["message" => $this->message];
    }

    public function broadcastAs()
    {
        return 'message';
    }
}
<template>
  <div class="composer">
    <textarea v-model="message" @keydown.enter="send" placeholder="Message..."></textarea>
  </div>
</template>
<script>
export default {
  data() {
    return {
      message: ""
    };
  },
  methods: {
    send(e) {
      e.preventDefault();

      if (this.message == "") {
        return;
      }

      this.$emit("send", this.message);
      this.message = "";
    }
  }
};
</script>

<style>
.composer textarea {
  width: 96%;
  margin: 10px;
  resize: none;
  border-radius: 3px;
  border: 1px solid lightgray;
  padding: 6px;
}
</style>

这是我尝试过的。一切正常。我只需要刷新页面,但我希望它不刷新页面。我要实时的。请让我知道我的代码哪里错了。我不知道,我按照在线教程进行操作,效果很好,但我不知道我的代码问题出在哪里。任何帮助将不胜感激。

【问题讨论】:

  • 我觉得你需要实现一些Pusher code
  • 我已经安装并设置了推送器,我可以在调试控制台中看到我的消息到达了我的推送器帐户,只是它现在实时显示给其他用户。

标签: laravel pusher laravel-echo


【解决方案1】:

您还没有订阅您的频道,也没有在您的代码中收听您的事件。

【讨论】:

  • 您好,我想知道您需要什么代码,以便我可以编辑我的代码并将其展示给您,对不起,我是新手。
猜你喜欢
  • 1970-01-01
  • 2019-12-06
  • 1970-01-01
  • 2020-11-05
  • 2017-07-18
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2017-08-13
相关资源
最近更新 更多