客服系统有个需求,就是右下角需要实时出现客服创建的提醒,比如几点几点给某某客户回拨电话。看了下之前系统的实现,客户端每分钟以 stomp 读取 ActiveMQ 中属于登录客服的信息。下面就把安装、配置及简单 demo 演示下
ActiveMQ 是 Java 编写的,因此需要安装 Java
yum -y list java*
yum -y install java-1.8.0-openjdk*
安装 ActiveMQ
cd /usr/local
wget https://archive.apache.org/dist/activemq/5.12.1/apache-activemq-5.12.1-bin.tar.gz
tar -xf apache-activemq-5.12.1-bin.tar.gz
chmod -R 755 apache-activemq-5.12.1
# 启动
/usr/local/apache-activemq-5.12.1/bin/activemq start
#启动成功后可以通过 web 方式访问,默认端口为 8161,账号密码为 admin/admin
安装 stomp 扩展
cd /usr/local
wget http://pecl.php.net/get/stomp-1.0.5.tgz
tar zxvf stomp-1.0.5.tgz
cd stomp-1.0.5
phpize
./configure
make
make install
echo "extension = stomp.so" >> /etc/php.d/php-stomp.ini
service php-fpm restart
数据写入 ActiveMQ
<?php
$notice = new stdClass();
$notice->reply_user_id=2;
$notice->content=8901;
$notice->type='remind';
$notice->mark=1;
$notice->sender='';
$notice->deal_url='';
$notice_str = json_encode($notice);
$raw_time = '2018-07-18 17:16:01';
$header = array('show_time'=>strtotime($raw_time),'receiver'=>2,'mark'=>1,'raw_time' => $raw_time);
try
{
$stomp = new Stomp("tcp://127.0.0.1:61612");
$stomp->send('/queue/msg', $notice_str, $header);
unset($stomp);
}
catch(StompException $e)
{
echo $e->getMessage();
}
实时读取 ActiveMQ
<?php
$stomp = new Stomp('tcp://127.0.0.1:61612');
$stomp->subscribe('/queue/msg');
while(true) {
$stomp->setReadTimeout(30);
$now= time()+60;
$stomp->subscribe('/queue/msg', array('selector'=>"receiver=2 AND show_time<=$now"));
$notice_amq = $stomp->readFrame();
$stomp->ack($notice_amq); // 删除此 mq 消息
var_dump($notice_amq);
unset($stomp);
}