首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pusher :App\Events\Events不回调testChannel

Pusher :App\Events\Events不回调testChannel
EN

Stack Overflow用户
提问于 2018-07-12 10:35:52
回答 2查看 2.7K关注 0票数 0

我建立了一个Laravel广播系统,计划将其作为一个实时聊天应用程序来实现。当检查客户端页面时,控制台日志显示:

Pusher :事件记录:{"event":"App\\Events\\Event","data":{"message":"Greetings from PrinceLuo!"},"channel":"testChannel"} Pusher :App\Events\Events不回调testChannel

它只是忽略了回调函数.

顺便说一下,我还没有安装npm,所以我使用了Pusher仪表板建议的简单Javascript代码,而不是Laravel建议的Vue代码。

在控制台日志和Pusher仪表板上,我都可以看到服务器发送的广播消息。

这是我的客户端代码:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
    <!DOCTYPE html>
<head>
  <title>Pusher Test</title>

  <script src="{{ asset('js/pusher.min.js') }}"></script>
  <script>

    // Enable pusher logging - don't include this in production
    Pusher.logToConsole = true;

    var pusher = new Pusher('****************', {
      cluster: 'ap1',
      encrypted: true
    });

    var channel = pusher.subscribe('testChannel');
    channel.bind('App\Events\Event', function(data) {
  console.log(data);
});


  </script>
</head>
<body>
  <h1>Pusher Test</h1>
  <p>
    Try publishing an event to channel <code>testChannel</code>
    with event name <code>Event</code>.
  </p>
</body>
</html>

把推子的钥匙藏起来~~

我搜索过一些类似的案子。但没人能给我答复。有没有人见过这个案子,或者对这个案子有什么想法?

更新:

我还在这里发布了我的服务器端代码,供任何需要的人使用:

代码语言:javascript
复制
<?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 Event implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

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

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

这是我的路线:

代码语言:javascript
复制
Route::get('test_event',function(){
    event(new Event('Greetings from PrinceLuo!'));
});

Route::get('test_listen',function(){
    return view('listenBroadcast');
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-13 17:58:35

对于那些感兴趣的人,我张贴了我为解决这个问题所做的工作:

注意,Push Logger显示[App\Events\Event]以转义反斜杠的函数。因此,在JavaScript中,我们必须对其进行相同的修改:

代码语言:javascript
复制
channel.bind('App\\Events\\Event', function(data){});

很简单但很重要。

票数 2
EN

Stack Overflow用户

发布于 2020-06-20 07:37:42

这就是我所做的

代码语言:javascript
复制
channel.bind('pusher:subscription_succeeded', function(members) {
    // alert('successfully subscribed!');
 });

 channel.bind("App\\Events\\NewComment", function(data) {
     console.log(data);
 });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51303435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档