Laravel laravel-echo-server 没有收到消息(聊天)
Laravel laravel-echo-server not receiving messages (chat)
我正在尝试设置 laravel-echo-server but I can't get it to work. With the same setup (I use Vue.js) it's working with pusher。我建立了一个聊天室。
我的 laravel-echo-server.json
看起来像这样:
{
"authHost": "https://sayhidog.test",
"authEndpoint": "/broadcasting/auth",
"clients": [],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "https",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "/Users/lars/.config/valet/Certificates/sayhidog.test.crt",
"sslKeyPath": "/Users/lars/.config/valet/Certificates/sayhidog.test.key",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}
当我运行laravel-echo-server start
我看到:
这看起来很棒,当我发送消息时我看到:
在vue.js中我这样连接:
window.io = require('socket.io-client');
if (typeof io !== 'undefined') {
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
});
}
我是这样听的:
window.Echo.private('chat')
.listen('MessageSent', (e) => {
this.messages.push({
body: e.message.body,
user: e.user
});
});
我的 channels.php 看起来像这样:
Broadcast::channel('chat', function ($user) {
return true;
});
在我的控制器中我广播:
broadcast(new MessageSent($user, $message))->toOthers();
MessageSent 看起来像这样:
class MessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* User that sent the message
*
* @var User
*/
public $user;
/**
* Message details
*
* @var ChatMessage
*/
public $message;
/**
* Create a new event instance.
*
* @param User $user
* @param ChatMessage $message
*/
public function __construct(User $user, ChatMessage $message)
{
$this->user = $user;
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('chat');
}
}
我的网络选项卡中的连接看起来不错:
我的聊天中没有收到任何新消息。有了推动器,它就可以工作了。我在这里做错了什么?谢谢!
首先在名为 broadcastAs 的 MessageSent 频道上定义方法,它 return 自定义名称如下:
public function broadcastAs()
{
return 'message.sent';
}
然后使用您选择的自定义名称收听此事件,并在前面加上点 (.),如下所示:
window.Echo.private('chat')
.listen('.message.sent', (e) => {
this.messages.push({
body: e.message.body,
user: e.user
});
});
尝试在 .env 文件中添加这一行,这解决了我的问题:
REDIS_PREFIX=
检查您的 package.json 以确保您已安装 socket.io-客户端版本 2.3.0。目前(2021 年 1 月)3.0 版将无法使用,因为我花了很多时间发现。
我正在尝试设置 laravel-echo-server but I can't get it to work. With the same setup (I use Vue.js) it's working with pusher。我建立了一个聊天室。
我的 laravel-echo-server.json
看起来像这样:
{
"authHost": "https://sayhidog.test",
"authEndpoint": "/broadcasting/auth",
"clients": [],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "https",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "/Users/lars/.config/valet/Certificates/sayhidog.test.crt",
"sslKeyPath": "/Users/lars/.config/valet/Certificates/sayhidog.test.key",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}
当我运行laravel-echo-server start
我看到:
这看起来很棒,当我发送消息时我看到:
在vue.js中我这样连接:
window.io = require('socket.io-client');
if (typeof io !== 'undefined') {
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
});
}
我是这样听的:
window.Echo.private('chat')
.listen('MessageSent', (e) => {
this.messages.push({
body: e.message.body,
user: e.user
});
});
我的 channels.php 看起来像这样:
Broadcast::channel('chat', function ($user) {
return true;
});
在我的控制器中我广播:
broadcast(new MessageSent($user, $message))->toOthers();
MessageSent 看起来像这样:
class MessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* User that sent the message
*
* @var User
*/
public $user;
/**
* Message details
*
* @var ChatMessage
*/
public $message;
/**
* Create a new event instance.
*
* @param User $user
* @param ChatMessage $message
*/
public function __construct(User $user, ChatMessage $message)
{
$this->user = $user;
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('chat');
}
}
我的网络选项卡中的连接看起来不错:
我的聊天中没有收到任何新消息。有了推动器,它就可以工作了。我在这里做错了什么?谢谢!
首先在名为 broadcastAs 的 MessageSent 频道上定义方法,它 return 自定义名称如下:
public function broadcastAs()
{
return 'message.sent';
}
然后使用您选择的自定义名称收听此事件,并在前面加上点 (.),如下所示:
window.Echo.private('chat')
.listen('.message.sent', (e) => {
this.messages.push({
body: e.message.body,
user: e.user
});
});
尝试在 .env 文件中添加这一行,这解决了我的问题:
REDIS_PREFIX=
检查您的 package.json 以确保您已安装 socket.io-客户端版本 2.3.0。目前(2021 年 1 月)3.0 版将无法使用,因为我花了很多时间发现。