Laravel回声不听 [英] Laravel echo is no listening
问题描述
我来自委内瑞拉,尝试将laravel echo与一个项目集成时遇到问题,我尝试了一切,想得到您的帮助.
i'm from venezuela, i've a problem tryging to integrate laravel echo with a project, i try everything and i wanna receive your help.
也许我的方法不正确.
我正在尝试使用带有套接字io的广播事件,因为我在一家初创公司工作,并且很难支付诸如pusher之类的服务.
i'm trying to use broadcasting events with socket io because i'm working in a startup and is very difficult pay a services like pusher.
我正在尝试进行简单的实时聊天.
i'm trying to do a simple real time chat..
那是我的chatController.
That is my chatController..
public function index(Request $request){
$sender=Auth::user()->id;
$receiver=\App\User::find(2);
$message=new Message;
$message->message="Hola, esto es una prueba de eventos";
$message->user_receiver_id=$receiver->id;
$message->user_sender_id=Auth::user()->id;
$message->id_chat=1;
$message->save();
event(new \App\Events\sendMessage($message,$receiver));
return response()->json(["status"=>"ok"]);
}
此聊天室根据确定的组(聊天)将消息存储在数据库中.
This chat store in a database the messages according to determinate group (chat).
将要触发的事件是:
class sendMessage实现ShouldBroadcast{使用Dispatchable,InteractsWithSockets,SerializesModels;
class sendMessage implements ShouldBroadcast { use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $message;
public $user_receiver;
public function __construct(Message $message,User $user_receiver)
{
$this->message=$message;
$this->user_receiver=$user_receiver;
}
public function broadcastOn()
{
\Log::info($this->message->id_chat);
return new PrivateChannel('chat.1');
}}
我的app.js是:
require('./bootstrap');
import Echo from "laravel-echo";
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001'
});
window.Echo.channel('chat.1')
.listen('sendMessage', e => {
console.log(e);
});
首先,我只是想从我的sendMessages事件中获取公共变量,此后,我将构建视图.
at the first instance i just wanna get the public variables from my sendMessages event, after this i will to build the view.
我运行php artisan侦听器,并运行de laravel回显服务器.
I run the php artisan listener and i run de laravel echo server.
当我发送消息时,laravel回显服务器和侦听器会发生这种情况:
when i send a message this happen with the laravel echo server and the listener:
我认为该过程运行良好,但是我认为我与客户之间只有一个问题.
I think that the process work fine but i think that i only have a problem with the client..
因为当我发送消息时客户端中什么也没发生.
because nothing happen in the client when i send the message.
对不起,我的英文,希望您能对我有所帮助.
Sorry for my english, i hope that you can help me.
推荐答案
如果没有完整的源代码和运行应用程序,很难解决此问题.
This problem is very hard to troubleshoot without complete source code and running app.
我已经在此聊天应用程序中实现了所有三种类型的渠道(私人,公共和在线),也许您可以从中获得一些想法:
I have implemented all 3 types of channels(private, public & presence) in this chat app, maybe you can get some ideas from it:
https://github.com/xparthx/laravel-realtime-chat
谢谢
这篇关于Laravel回声不听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!