gpt4 book ai didi

php - 带套接字 io 和 redis 的 laravel echo 服务器

转载 作者:IT王子 更新时间:2023-10-29 06:13:18 25 4
gpt4 key购买 nike

我尝试使用 laravel-echo 服务器和 socket.io 创建实时应用程序,但客户端没有成为消息

这是我的代码:

.环境

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=redis

配置/queue.php

 'redis' => [
'driver' => 'redis',
'connection' => 'default',
'queue' => 'default',
'retry_after' => 90,
],

配置/数据库.php

'redis' => [

'retry_after' => 90,
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],

],

routes/channels.php

Broadcast::channel('messages', function() {
return true;
});

src/echo.js

import Echo from "laravel-echo"

window.Echo = new Echo({
broadcaster: 'socket.io',
host:'http://localhost:6001'
});

window.Echo.channel('messages')
.listen('.newMessage', (message) => {
debugger;
});

事件/MessagePosted.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class MessagePosted implements ShouldBroadcast {

use Dispatchable, InteractsWithSockets, SerializesModels;

protected $message;

public function __construct($message)
{
$this->message = $message;
}

public function broadcastWith()
{
return [
'message' => $this->message,
];
}

public function broadcastAs()
{
return 'newMessage';
}

public function broadcastOn()
{
return new Channel('messages');
}
}

MessageController.php

public function post(Request $request)
{
event(new MessagePosted($request->get('message')));
}

Redis服务器安装完毕,在redis-cli中PING命令返回PONG。我使用 php artisan queue:work redis,但是从客户端发送消息后,队列中没有显示任何内容...客户端连接到 laravel-echo-server,返回: [14:04:17] - C3c8UUnTn9dvOPc9AAAA 加入 channel :消息。并处理POST请求

最佳答案

您正在收听 .newMessage 但发送的是 .App.Events.MessagePosted

更改 JS 中的监听函数参数:

window.Echo.channel('messages')
.listen('.App.Events.MessagePosted', (message) => {
debugger;
});

关于php - 带套接字 io 和 redis 的 laravel echo 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46095326/

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