【问题标题】:How can I find the information sent from the laravel websocket web server?如何找到从 laravel websocket 网络服务器发送的信息?
【发布时间】:2023-03-19 02:59:01
【问题描述】:

我使用 websocket 服务器将事件的信息作为广播发送。 但我无法通过 laravel echo 获取信息。 这是我对 laravel 和 websocket 服务器的配置。

广播配置

    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
        'options' => [
            'cluster' => env('PUSHER_APP_CLUSTER'),
            // 'encrypted' => true,
            'host' => '127.0.0.1',
            'port' => 6001,
            'scheme' => 'http',
            //disable cros orgin
            'useTLS' => false,
        ],
    ],

websockets 配置

 'apps' => [
    [
        'id' => env('PUSHER_APP_ID'),
        'name' => env('APP_NAME'),
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'path' => env('PUSHER_APP_PATH'),
        'capacity' => null,
        'enable_client_messages' => false,
        'enable_statistics' => true,
    ],
],

环境配置

PUSHER_APP_ID=your-pusher-app-id
PUSHER_APP_KEY=your-pusher-key
PUSHER_APP_SECRET=your-pusher-secret
PUSHER_APP_CLUSTER=mt1
LARAVEL_WEBSOCKETS_PORT=6001

boostrap.js

window.Echo = new Echo({
broadcaster: 'pusher',
key: "your-pusher-key",
cluster: "mt1",
wsHost: window.location.hostname,
wsPort: 6001,

//two config above disable crose orgin
disableStats: true,
forceTLS: false,

});

我的活动档案

class OrderStatusChange implements ShouldBroadcast{ 

use Dispatchable, InteractsWithSockets, SerializesModels;

public $message;
public $user;

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


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

}

和 laravel-echo

 window.Echo.channel('notifications').listen('OrderStatusChange', (e) => {
console.log(e.data);
console.log(e);
console.log('window');

});

和控制台

Pusher :  : ["State changed","initialized -> connecting"] app.js:38851:32

Pusher : : ["Connecting",{"transport":"ws","url":"ws://shop.local:6001/app/your-pusher-key?protocol=7&client=js&version=7.0 .2&flash=false"}] app.js:38851:32 Pusher : : ["State changed","connecting -> connected with new socket ID 307045135.646205772"] app.js:38851:32 Pusher : : ["事件发送",{"event":"pusher:subscribe","data":{"auth":"","channel":"order"}}] Pusher : : ["Event recd",{"event":"pusher_internal:subscription_succeeded","channel":"order"}] app.js:38851:32 Pusher : : ["对于 pusher:subscription_succeeded 订单没有回调"]

【问题讨论】:

标签: php laravel events websocket


【解决方案1】:

你在event 中使用broadcastWith 函数吗?

在此函数中,您可以定义要广播的数据。 此外,请确保您的 WebSocket 已成功连接。

【讨论】:

  • 连接成功。并且我可以通过web socket面板发送和接收信息,但是我不能自己发送和接收事件的信息。
【解决方案2】:

谢谢大家 我发现使用的解决方案写了答案可能对某人有帮助 只需运行 queue,如果您不需要队列,只需在 env 文件中 将 QUEUE_CONNECTION 的值更改为 sync 并且还在事件类中使用 ShouldBroadcastNow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2011-11-11
    • 2021-01-28
    • 2020-10-30
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多