简体   繁体   English

如何将事件发送给特定用户? (laravel 5.3)

[英]How to send an event to a particular user? (laravel 5.3)

I am building one to one chat with laravel and pusher.I am trying to send event only to the person one is talking to but not able to do so.Event is send to all ,don't know what is the issue. 我正在与laravel和pusher建立一对一的聊天。我试图将事件仅发送给正在与之交谈的人,但无法发送。事件被发送给所有人,不知道是什么问题。

<ul class="list" id="friend">
      @foreach(Auth::user()->friends() as $f)
        <li  data-friend="{{$f->id}}" class="clearfix" style="cursor: pointer">
          <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/195612/chat_avatar_01.jpg" alt="avatar" />
          <div class="about">
            <div class="name">{{$f->firstname}}</div>
            <div class="status">
              <i class="fa fa-circle online"></i> online
            </div>
          </div>
        </li>
        @endforeach
      </ul>

textarea: 文字区域:

<div class="chat-message clearfix">
        <textarea name="message-to-send" id="message-to-send" placeholder ="Type your message" rows="3"></textarea>

        <i class="fa fa-file-o"></i> &nbsp;&nbsp;&nbsp;
        <i class="fa fa-file-image-o"></i>

        <button id="send" data-userid="{{Auth::user()->id}}">Send</button>

      </div>

script: 脚本:

<script>
      b={!!json_encode(Auth::user()->id)!!};
     pusher=new Pusher('******',{
    cluster:'ap1'
   });
  channel=pusher.subscribe('user'+b);

    $(document).ready(function(){
   $('#friend li').click(function(){
   var f=$(this).attr('data-friend');
    $('#send').click(function(){
    var userid=$(this).attr('data-userid');
    var message=$('#message-to-send').val();

      $.ajax({
        method:'GET',
        url:'/chat2/'+message+'/'+f,
        success:function(response){
          console.log('dshfgjhgdhjs');
        }
      })
  });
});
 });
     channel.bind('App\\Events\\chat2', function(e){
      console.log(e);
     } );
  </script>

route: 路线:

Route::get('/chat2/{message}/{friend}',function($message,$friend){

     event(new chat2(Auth::user(),$message,$friend));

});

event: 事件:

class chat2 implements ShouldBroadcast
{
    use InteractsWithSockets, SerializesModels;

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

    /**
     * Get the channels the event should broadcast on.
     *
     * @return Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('user'.$this->friend);
    }
}

You need to look at private broadcast channels. 您需要查看私人广播频道。 Laravel supports authenticated private channels for broadcast events. Laravel支持经过身份验证的广播事件专用频道。

https://laravel.com/docs/5.4/broadcasting#authorizing-channels https://laravel.com/docs/5.4/broadcasting#authorizing-channels

Once you have the user listening on a private channel, you can send events only to that channel. 一旦用户在私人频道上收听,就只能将事件发送到该频道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM