最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - I can't receive events via reverb on the local network - Stack Overflow

programmeradmin2浏览0评论

I created an application with reverb where I made a simple channel and an event that receives a message and transmits it to the subscribers on the reverb channel. It turns out that when I tried to make a connection with the frontend in React using echo.js I ended up not receiving any type of information from the channel. How can I implement reverb correctly? The documentation does not have the information I am looking for.

Reverb log: INFO Starting server on 0.0.0.0:8080 (192.168.1.69)

Laravel Event:

<?php

namespace App\Events;

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

class Chat implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $msg;
    /**
     * Create a new event instance.
     */
    public function __construct($msg)
    {
        //
        $this->msg = $msg;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new Channel('chat'),
        ];
    }
}

Laravel Channel:

<?php

use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});

Broadcast::channel('Chat', function () {
});

Call:

Route::get('/ws', function(){
    Broadcast(new Chat('test'));
});

Echo.js front-react

import Echo from 'laravel-echo';
import Pusher from 'pusher-js';

const echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.REACT_APP_PUSHER_APP_KEY, // Chave do Pusher
    wsHost: '192.168.1.69', // Host do Reverb
    wsPort: 8080, // Porta do Reverb
    forceTLS: false, 
    disableStats: true,
});

export default echo;

app.js

import React, { useEffect, useState } from 'react';
import echo from './echo'; // Importe o Echo configurado

function App() {
    const [mensagem, setMensagem] = useState('');

    useEffect(() => {
        // Ouvir um canal público
        echo.channel('chat')
            .listen('Chat', (e) => {
                setMensagem(e.mensagem); // Atualize o estado com a mensagem recebida
                console.log('Evento recebido:', e);
            });

        // Limpar a inscrição ao desmontar o componente
        return () => {
            echo.leaveChannel('canal-reverb');
        };
    }, []);

    return (
        <div>
            <h1>Mensagem Recebida:</h1>
            <p>{mensagem}</p>
        </div>
    );
}

export default App;

I can successfully receive connections from the machine where the local server is located, but when I try to make connections with other machines, Reverb does not identify connections.

It runs normally on the local machine.

发布评论

评论列表(0)

  1. 暂无评论