gpt4 book ai didi

php - Laravel laravel-echo-server未收到消息(聊天)

转载 作者:行者123 更新时间:2023-12-03 11:52:51 25 4
gpt4 key购买 nike

我正在尝试设置laravel-echo-server,但无法正常工作。使用相同的设置(我使用Vue.js),它可以与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时,我看到:

enter image description here

看起来很不错,当我发送一条消息时,我看到:

enter image description here

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

在我的 Controller 中,我广播了:
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');
}

}

我的网络标签中的连接看起来不错:

enter image description here

我的聊天中没有收到任何新消息。使用推杆,它可以正常工作。我在这里可能做错了什么?谢谢!

最佳答案

首先在您的MessageSent channel 上定义一个名为broadcastAs的方法,并返回如下的自定义名称:

public function broadcastAs()
{
return 'message.sent';
}

然后使用您选择的自定义名称收听此事件,并以点(.)开头,如下所示:
window.Echo.private('chat')
.listen('.message.sent', (e) => {
this.messages.push({
body: e.message.body,
user: e.user
});
});

关于php - Laravel laravel-echo-server未收到消息(聊天),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57893256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com