Php 如何向特定用户发送事件?(拉威尔5.3)
我正在与laravel和pusher建立一对一的聊天。我试图只将事件发送给与我交谈的人,但无法发送。事件发送给所有人,不知道问题出在哪里Php 如何向特定用户发送事件?(拉威尔5.3),php,laravel,events,laravel-5,laravel-5.3,Php,Laravel,Events,Laravel 5,Laravel 5.3,我正在与laravel和pusher建立一对一的聊天。我试图只将事件发送给与我交谈的人,但无法发送。事件发送给所有人,不知道问题出在哪里 <ul class="list" id="friend"> @foreach(Auth::user()->friends() as $f) <li data-friend="{{$f->id}}" class="clearfix" style="cursor: pointer">
<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>
活动:
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);
}
}
你需要看看私人广播频道。Laravel支持广播事件的经过身份验证的专用频道
一旦用户在私人频道上收听,您只能向该频道发送事件。而不是“事件发送给所有人”您的意思是“事件发送给所有人”?@MhluziBhaka是的!
<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::get('/chat2/{message}/{friend}',function($message,$friend){
event(new chat2(Auth::user(),$message,$friend));
});
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);
}
}