Pusher Undefined 属性: Laravel Lumen 中的 stdClass::$channels

Pusher Undefined property: stdClass::$channels in Laravel Lumen

我正在使用 Lumen 和 Vue.JS 制作 POC。现在它只需要从 Lumen 后端向 Vue.JS 前端发送一条“hello world”消息(有效)。我做了一个在加载页面时触发的事件,如下所示:

public function sendMessage(Request $request)
{
    event(new MessageEvent('hello world'));
}

MessageEvent 看起来像这样(从 Pusher 入门帮助中获取):

class MessageEvent extends Event implements ShouldBroadcast
{
    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $message;

    public function __construct($message)
    {
        $this->message = $message;
    }
  
    public function broadcastOn()
    {
        return ['my-channel'];
    }
  
    public function broadcastAs()
    {
        return 'my-event';
    }
}

这部分工作正常,因为我在 Vue.JS 应用程序中收到了这个:

Pusher :  : ["Event recd",{"event":"my-event","channel":"my-channel","data":{"message":"hello world"}}]

现在我检查队列日志时出现了问题。用 php artisan queue:listen 触发,我看到以下内容:

[2021-03-14 11:57:03][Bh7373O9EETAZc39M2RCSPmUTjwSbSmL] Processing: App\Events\MessageEvent
[2021-03-14 11:57:04][Bh7373O9EETAZc39M2RCSPmUTjwSbSmL] Failed:     App\Events\MessageEvent

当我检查 Lumen 日志文件时,它显示以下内容:

[2021-03-14 11:43:12] local.ERROR: Undefined property: stdClass::$channels {"exception":"[object] (ErrorException(code: 0): Undefined property: stdClass::$channels at /var/www/vendor/pusher/pusher-php-server/src/Pusher.php:538)

所以我继续检查 Pusher.php 文件:

536:         $result = json_decode($response['body']);
537:         
538:         if ($result->channels) {
539:             $result->channels = get_object_vars($result->channels);
540:         }

我决定检查一下 $response 是什么,它给出了以下内容:

[2021-03-14 11:57:04] local.INFO: array (
  'body' => '{}',
  'status' => 200,
)  

如果response["body"]["channels"]不存在,当然无法到达$result->channels

当我去查看 Pusher API 参考资料时,它说了以下内容:

这意味着正文确实应该包含 JSON 响应。但是当我进一步滚动时,我看到了这个:

这应该意味着您不必设置信息参数,因为它是可选的和实验性的。

[EXPERIMENTAL] If the info parameter is sent, then it returns a hash of unique channels that were triggered to. The hash maps from channel name to a hash of attributes for that channel (may be empty).

它对 info 参数集的预期响应是这样的:

{
  "channels": {
    "presence-foobar": {
      "user_count": 42,
      "subscription_count": 51
    },
    "presence-another": {
      "user_count": 123,
      "subscription_count": 140
    },
    "another": {
      "subscription_count": 13
    }
  }
}

请求的 channels 对象是哪个。

我的问题是,我是不是错过了什么,或者这是 Pusher 的错误?我真的在这个问题上断了一条腿,所以我希望有人能帮助我。

推手API参考:https://pusher.com/docs/channels/library_auth_reference/rest-api

修复composer.json

我在 PHP 包上创建了一个问题:https://github.com/pusher/pusher-http-php/issues/295

这个版本确实损坏了,但修复应该在 composer.json 文件中。我的看起来像这样:

{
    ...
    "require": {
        ...
        "pusher/pusher-php-server": "5.0"
        ...
    },
    ...
}

这意味着我特意说它应该使用版本5.0。但是现在我不会得到版本 5.2 例如,这意味着我不会得到补丁。根据在 Github 上回答我的问题的人的说法,我应该更改我的 composer.json 并在版本号前添加一个 ^ 以便它确实获得该版本的补丁。所以应该改成这样:

{
    ...
    "require": {
        ...
        "pusher/pusher-php-server": "^5.0"
        ...
    },
    ...
}

之后别忘了运行composer update

根据 Graham Campbell 在 Github 上的说法:

Anyone who puts 5.0 in their composer.json file has made an error, since composer resolves this to the version 5.0.0.0 and not the version constraint ^5.0.

修复pusher.php(不推荐)

另一种解决方法是直接编辑 /vendor/pusher/pusher-php-server/src/Pusher.php。虽然不推荐,但确实有效。

536:         $result = json_decode($response['body']);
537:         
538:         if ($result->channels) {
539:             $result->channels = get_object_vars($result->channels);
540:         }

这不起作用,因为 channels 不存在于 result 对象中。它应该首先检查通道对象是否存在。您可以通过将上面的代码更改为:

536:         $result = json_decode($response['body']);
537:         
538:         if (property_exists($result, 'channels')) {
539:             $result->channels = get_object_vars($result->channels);
540:         }