【发布时间】:2015-01-26 03:16:31
【问题描述】:
我正在尝试使用 laravel 和 pusher 在用户和管理员之间创建一对一的响应。用户没有登录,只是一个访客。当用户访问该站点时,他们会输入一些内容并将其发送给管理员。如果任何管理员在线,他们将单击是或否,然后这将被发送回发送它的用户。
我的问题是我似乎无法让推送者发送回复。我看过这个Pusher one to one chat structure,但我不明白,因为在我的场景中它不是聊天室,而且更具体到聊天室。
无论如何,我都尝试使用会话 ID,但这不起作用,因为用户和管理员不在同一个浏览器上。我想过以某种方式发送带有推送通知消息的会话 ID,但我也知道如何发送,因为管理员在 /admin/events 页面上,而用户在主页上。
这是我的代码
push.js
var pusher = new Pusher('MY KEY');
var channel = pusher.subscribe ('client');
channel.bind('general_response', addMessage);
var channel = pusher.subscribe ('customer_response');
//bind to new_response on the channel
channel.bind('message', addWaitingResponse);
//user
function addMessage (data) {
//create list item
var li = $('<div class="response_message"></div>');
//Get text properity from data
li.text(data.text);
//hide it
li.hide();
//prepend to messages
$('#messages').prepend(li);
li.slideDown();
}
//admin
function addWaitingResponse(data) {
var li = $('<div class="waiting_approve"> </div> ');
li.text(data.text);
li.hide();
$('#WaitingNotification').prepend(li);
li.slideDown();
}
订阅和收听
<?php
namespace Scan\Subscriber;
class NotificationEventSubscriber {
private $_pusher;
public function __construct() {
$this->_pusher = new \Pusher($_ENV['app_key'], $_ENV['app_secret'], $_ENV['app_id']);
}
//message to admin
public function onCreate($event) {
//$pusher = new \Pusher($_ENV['app_key'], $_ENV['app_secret'], $_ENV['app_id']);
$data = array('text' => $event->track_no 'is awaiting approval');
$this->_pusher->trigger('customer_response', 'message', $data);
}
//message to user
public function onUpdate($event) {
/*
| Notify customer
*/
$message = array('text' => $event->track_no' has been approved ');
$this->_pusher->trigger('client', 'general_response', $message);
}
public function subscribe($events) {
$events->listen('notification.insert','Scan\Subscriber\NotificationEventSubscriber@onCreate');
$events->listen('notification.update','Scan\Subscriber\NotificationEventSubscriber@onUpdate');
}
}
当用户发送请求时,我在 laravel 中注册并触发事件。上面的代码只是显示了我如何向用户和管理员显示消息
我的问题是:如何使管理员响应仅发送给发送请求的用户?非常感谢。
【问题讨论】:
标签: javascript php asp.net laravel pusher