简介
web-msg-sender是一款web长连接推送框架,采用PHPSocket.IO开发,基于WebSocket长连接通讯,如果浏览器不支持WebSocket则自动转用comet推送。 通过后台推送消息,消息可以即时推送到客户端,非轮询,实时性非常好,性能很高。
特点:
多浏览器支持
支持针对单个用户推送消息
支持向所有用户推送消息
长连接推送(websocket或者comet),消息即时到达
支持在线用户数实时统计展示
支持在线页面数实时统计展示
支持跨域推送
场景
我司客服部门需要订单产生变化时方便的收到提醒,同时会员也需要站内信此类功能
环境
- php7
- thinkphp 5
- ubuntu 16.04, Deepin 15.2, CentOS 7
~~安装~~
基本上下载下来就好了https://github.com/walkor/web-msg-sender
服务跑起来
cd web-msg-sender
php start.php start -d
然后就会看见如下输出
Workerman[start.php] start in DAEMON mode
----------------------- WORKERMAN -----------------------------
Workerman version:3.2.8 PHP version:7.0.8-2
------------------------ WORKERS -------------------------------
user worker listen processes status
sinux PHPSocketIO socketIO://0.0.0.0:2120 1 [OK]
sinux WebServer http://0.0.0.0:2123 1 [OK]
----------------------------------------------------------------
Input "php start.php stop" to quit. Start success.
这就是跑起来了,OK
demo
服务端推
/application/lib/event/PushEvent.php
namespace app\lib\event;
/**
* 推送事件
* 典型调用方式:
* $push = new PushEvent();
* $push->setUser($user_id)->setContent($string)->push();
*
* Class PushEvent
* @package app\lib\event
*/
class PushEvent
{
/**
* @var string 目标用户id
*/
protected $to_user = '';
/**
* @var string 推送服务地址
*/
protected $push_api_url = 'http://127.0.0.1:2121/';
/**
* @var string 推送内容
*/
protected $content = '';
/**
* 设置推送用户,若参数留空则推送到所有在线用户
*
* @param string $user
* @return $this
*/
public function setUser($user = '')
{
$this->to_user = $user ? : '';
return $this;
}
/**
* 设置推送内容
*
* @param string $content
* @return $this
*/
public function setContent($content = '')
{
$this->content = $content;
return $this;
}
/**
* 推送
*/
public function push()
{
$data = [
'type' => 'publish',
'content' => $this->content,
'to' => $this->to_user,
];
$ch = curl_init ();
curl_setopt($ch, CURLOPT_URL, $this->push_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
$res = curl_exec($ch);
curl_close($ch);
dump($res);
}
}
客户端(浏览器)收
/application/demo/view/PushDemo/targetPage.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<strong id="count"></strong>
<h1 id="target"></h1>
</body>
</html>
<script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.min.js"></script>
<script src='http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script>
<script>
jQuery(function ($) {
// 连接服务端
var socket = io('http://127.0.0.1:2120'); //这里当然填写真实的地址了
// uid可以是自己网站的用户id,以便针对uid推送以及统计在线人数
uid = 123;
// socket连接后以uid登录
socket.on('connect', function () {
socket.emit('login', uid);
});
// 后端推送来消息时
socket.on('new_msg', function (msg) {
console.log("收到消息:" + msg);
$('#target').append(msg).append('<br>');
});
// 后端推送来在线数据时
socket.on('update_online_count', function (online_stat) {
console.log(online_stat);
$('#count').html(online_stat);
});
})
</script>
写个用例,试一下
/application/demo/controller/PushDemo.php
namespace app\demo\controller;
use app\lib\event\PushEvent;
/**
* 推送demo
*
* Class PushDemo
* @package app\demo\controller
*/
class PushDemo
{
/**
* 推送一个字符串
*/
public function pushAString()
{
$string = 'Man Always Remember Love Because Of Romance Only';
$string = input('msg') ? : $string;
$push = new PushEvent();
$push->setUser()->setContent($string)->push();
}
/**
* 推送目标页
*
* @return \think\response\View
*/
public function targetPage()
{
return view();
}
}