vendredi 7 juin 2019

How to make POST to websocket route in Laravel 5.8?

I'm tryinng to create a listener when receiving a POST

POST to : http://site.test/websocket with data {"name":"john"}

I will broadcast {"name":"john"} to my console.log

I could get it to work.

I've tried

routes

Route::get('websocket', 'BroadcastController@refreshTest');
Route::post('websocket', 'BroadcastController@postTest');


TestEvent.php

<?php

namespace App\Events;

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

class TestEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    private $data;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('test');
    }

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


BroadcastController

<?php

namespace App\Http\Controllers;

use App\Events\TestEvent;
use Illuminate\Http\Request;

class BroadcastController extends Controller
{
    public function refreshTest()
    {
        broadcast(new TestEvent(['Jean', 'Malcom', 'John Doe']));
        return 'success';
    }

    public function postTest()
    {

        $json = 'John';
        broadcast(new TestEvent($json));
        return 'success';
    }
}


Result

I tried make a POST to http://site.test/websocket

I kept getting

enter image description here

My postTest() never seem to reach.

How do I debug this further ?



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2XyVvyW
via IFTTT

Aucun commentaire:

Enregistrer un commentaire