提问人:warmxd 提问时间:11/17/2023 更新时间:11/17/2023 访问量:34
Laravel Broadcasting with Soketi 通过 Echo 收听频道不起作用
Laravel Broadcasting with Soketi listening to channel via Echo is not working
问:
我正在将 Laravel Broadcasting 与 Soketi 一起使用,我正在尝试收听 jobs 频道和 JobFailedEvent。所以这是我的代码。
class JobFailedEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*/
public $jobName;
public $errorMessage;
public function __construct($jobName, $errorMessage)
{
$this->jobName = $jobName;
$this->errorMessage = $errorMessage;
}
/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new Channel('jobs'),
];
}
}
这是我的引导.js
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: "pusher",
key: import.meta.env.VITE_PUSHER_APP_KEY,
cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1',
wsHost:
import.meta.env.VITE_PUSHER_HOST ??
`ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`,
wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? "https") === "https",
enabledTransports: ["ws", "wss"],
});
最后是我尝试收听的 Vue 组件
const echo = ref();
echo.value = new Echo({
broadcaster: "pusher",
key: import.meta.env.VITE_PUSHER_APP_KEY,
cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER ?? 'mt1',
wsHost:
import.meta.env.VITE_PUSHER_HOST ??
`ws-${import.meta.env.VITE_PUSHER_APP_CLUSTER}.pusher.com`,
wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? "https") === "https",
enabledTransports: ["ws", "wss"],
});
echo.value.channel('jobs').listen('JobFailedEvent', (e) => {
console.log('Job failed:', e);
});
答: 暂无答案
评论