Javascript 无法侦听推送器专用频道事件?
我正试图设计一个聊天应用程序,在那里我试图通过推送者的私人频道传递消息,以便实时传递 我正在使用主题本身附带的聊天UI插件 基本上,我有Mapbox地图,我使用传单在地图上放置了很多标记,每个标记都是一个用户 因此,基本上当用户登录时,他可以查看地图上的其他用户,当他单击地图时,此代码执行:Javascript 无法侦听推送器专用频道事件?,javascript,jquery,laravel,leaflet,pusher,Javascript,Jquery,Laravel,Leaflet,Pusher,我正试图设计一个聊天应用程序,在那里我试图通过推送者的私人频道传递消息,以便实时传递 我正在使用主题本身附带的聊天UI插件 基本上,我有Mapbox地图,我使用传单在地图上放置了很多标记,每个标记都是一个用户 因此,基本上当用户登录时,他可以查看地图上的其他用户,当他单击地图时,此代码执行: // Binding the marker on click event to bring up the chat marker.on('click', function(){ var chatbo
// Binding the marker on click event to bring up the chat
marker.on('click', function(){
var chatboxId = 'private-' + currentUser.id + '-' + user.id;
chatboxManager.addBox(chatboxId, {
first_name: user.first_name,
last_name: user.last_name,
});
// Creating a new chatbox
$('#' + chatboxId).chatbox({
messageSent: function(id, user, message){
this.boxManager.addMsg('Me', message);
splittedId = id.split('-');
$.ajax({
url: '/messages',
method: 'post',
type: 'json',
data: {
receiver_id: splittedId[2],
body: message
}
});
}
});
// Initializing pusher and authenticating the private channel
var pusher = new Pusher('082bab423e2a8be3da2a', {
authTransport: 'jsonp',
authEndpoint: '/pusher/auth'
});
// Subscribing to the channel where the name of the channel is private-senderId-receiverId
var chat = pusher.subscribe('private-' + user.id + '-' + currentUser.id);
console.log(chat);
chat.bind('message', function(response){
console.log(response);
});
});
此代码为在地图上单击的用户生成一个新的聊天框。此时,pusher也被初始化,它会联系'/pusher/auth'进行私有通道的身份验证
身份验证工作正常。我知道这一点,因为pusher返回一个在JSON中应该返回的令牌。然后绑定通道变量以侦听“message”事件,但无法侦听通道上的新消息
此外,当我尝试console.log(chat)
(专用频道)时,它会在控制台中输出以下内容:
编辑服务器端代码,其中echo auth响应如下: 编辑此
echo $pusher->socket_auth($data['channel_name'], $data['socket_id']);
到
你能不能提供@leggetter的输出?我不知道这一点。我将对此进行调查并更新我的问题。谢谢。:)
Route::get('/pusher/auth', function(\Illuminate\Http\Request $request, \App\User $user){
$data = $request->all();
$explodedChannel = explode('-' ,$data['channel_name']);
if($user->currentUser()->id == $explodedChannel[2]){
$pusher = new \Pusher(
getenv('PUSHER_PUBLIC'),
getenv('PUSHER_PRIVATE'),
getenv('PUSHER_APPID')
);
echo $pusher->socket_auth($data['channel_name'], $data['socket_id']);
}
else{
return response()->json('Forbidden', 403);
}
});
echo $pusher->socket_auth($data['channel_name'], $data['socket_id']);
$auth= $pusher->socket_auth(Input::get('channel_name'), Input::get('socket_id'));
$callback = str_replace('\\', '', $_GET['callback']);
header('Content-Type: application/javascript');
echo($callback . '(' . $auth . ');');
return;