dtv8189
2018-04-08 14:42
浏览 64
已采纳

回声连接不接收来自广播在线频道的数据

I'm very basic at laravel's echo and pusher and I've been facing an issue despite the docs and tutorials. I'm trying to create a real time chat message feature in my website following a youtube video. Excerpts of code from my controller, model, channel and app.js are below respectively:

public function send(Request $request)
{
    $user = Auth::user();
    $id = Auth::user()->id;

    $message = $user->messages()->create([
        'user_id' => $id,
        'messages' => $request->get('messages')
    ]);
    broadcast(new MessagePosted($message, $user));
    return ['status' => 'Message Sent!'];
}

model:

 public function broadcastOn()
    {
//        return ['chatit'];
          return new PresenceChannel('chatit');
    }

channel:

Broadcast::channel('chatit', function ($user) {
    return true;
});

app:

window.Echo.join('chatit')
            .listen('MessagePosted', (e) => {
        console.log(e)
    });

My issue now is that if i return chatit (which i've commented) in my model, and change join to channel, i get to see the result of console.log, but when i use the presence channel instead, and use join in app.js, i only get message sent status. Any help will be appreciated.

图片转代码服务由CSDN问答提供 功能建议

我在laravel的回声和推动者方面非常基础,尽管有文档和教程,我一直面临着一个问题。 我正试图在我的网站上按照YouTube视频创建一个实时聊天消息功能。 我的控制器,模型,频道和app.js代码的摘录分别如下:

   public function send(Request $ request)
 {
 $ user = Auth :: user(); 
 $ id = Auth :: user() - > id; 
 
 $ message = $  user-> messages() - > create([
'user_id'=> $ id,
'messages'=> $ request-> get('messages')
]); 
  broadcast(new MessagePosted($ message,$ user)); 
 return ['status'=>  “已发送消息!”; 
} 
   
 
 

model:

  public function broadcastOn()
  {
 // return ['chatit']; 
返回新的PresenceChannel('chatit'); 
} 
   
 
 

频道: \ n

  Broadcast :: channel('chatit',function($ user){
 return true; 
}); 
   
 
 

app:

  window.Echo.join('chatit')
 .listen('MessagePosted',(e)=> {
 console.log(e  )
}); 
   
 
 

我现在的问题是,如果我在模型中返回chatit(我已评论过),并将join更改为channel,i 得到看到console.log的结果,但当我使用在线通道,并在app.js中使用join时,我只获得消息发送状态。 任何帮助将不胜感激。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dormbaf90464 2018-04-11 16:46
    已采纳

    I don't know why but after some hours of frustration, I restarted my pc and it just started working.

    I also found out that terminating and rerunning php artisan serve process could also solve the issue

    点赞 打赏 评论

相关推荐