dongtidai6519 2015-01-26 03:16
浏览 26
已采纳

使用推杆的一对一响应

I am trying to create a one to one response between a user and an admin using laravel and pusher. The user is not logged in, just a visitor. When the user visits the site, they will type something and send it to an admin. If any admin is online they will click yes or no and this will then get sent back to the USER who sent it.

My issue is that I can not seem to get pusher to send the responses. I have seen this Pusher one to one chat structure but i do not quiet understand as in my scenario it is not a chat room and that is more specific to a chat room.

Anyways I have tried to use the session id but that didn't work as the user and admin are not on the same browser. I thought about somehow sending the session id with the pusher notification message but I similar did know how because the admin is on a /admin/events page while the user is on the home page.

This is my code

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();
  }

subscribing and listening

<?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');

    }

}

In laravel when the user sends the requests, I register and fire the event. The code above just shows how I am displaying the message to the user and admin

My question is: How do I make the admin response ONLY sent to the user who sent the request? Thank you very much.

  • 写回答

1条回答 默认 最新

  • dou7466 2015-01-27 08:15
    关注

    It sounds like you are on the right lines. You need some way of creating a unique identifier that both the anonymous user and the admin user know about. That identifier can then be used to route messages - probably via a Pusher channel.

    Since the user is anonymous using the session ID as part of a unique channel name for that user seems like a good idea.

    Alternatively you could make the server respond with a unique ID when the anonymous user sends a request for help. With this in mind, here's a diagram showing a potential way of achieving this:

    Once the above process has completed the Admin and the anonymous User can now communicate by triggering events on the channel identified by the request_id.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数