web-msg-sender是一款web长链接推送框架,采用PHPSocket.IO开发,基于WebSocket长链接通信,若是浏览器不支持WebSocket则自动转用comet推送。 经过后台推送消息,消息能够即时推送到客户端,非轮询,实时性很是好,性能很高。php
下载3684次css
windows 参考 https://github.com/walkor/web-msg-sender-for-win前端
接收消息页面:http://www.workerman.net:2123/jquery
推送接口url:http://www.workerman.net:2121/?type=publish&to=uid&content=content
其中to为接收消息的uid,若是传空则向全部人推送消息 content 为消息内容 linux
见本页面页脚在线统计部分,支持实时的在线人数和在线页面统计git
一、下载web-msg-sender,并解压缩到任意目录github
命令行运行(此步骤包含了安装php-cli主程序以及pcntl、posix、libevent扩展及github程序)web
yum install php-cli php-process git gcc php-devel php-pear libevent-devel
二、启动workermanphp start.php start -d
以下图shell
三、浏览器访问端口http://ip:2123或者http://域名:2123,例如 http://workerman.net:2123如图:(若是没法访问请检查服务器防火墙)json
4.解压下载的文件 由于是zip文件须要下载linuxrar
http://www.rarsoft.com/download.htm
http://www.rarsoft.com/rar/rarlinux-x64-5.4.b4.tar.gz
tar zxvf rarlinux-xxxxxx
unzip 文件
make && make install
支持跨域推送,开发者能够不用创建服务端,直接使用线上的推送服务测试,只要引入js文件并设置下端口及回调便可,例如在任意站点中加入以下代码便可收到消息并统计数据:
<script src='http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script> <script> // 链接服务端 var socket = io('http://workerman.net: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); }); // 后端推送来在线数据时 socket.on('update_online_count', function(online_stat){ console.log(online_stat); }); </script>
<?php // 指明给谁推送,为空表示向全部在线用户推送 $to_uid = ""; // 推送的url地址,上线时改为本身的服务器地址 $push_api_url = "http://workerman.net:2121/"; $post_data = array( "type" => "publish", "content" => "这个是推送的测试数据", "to" => $to_uid, ); $ch = curl_init (); curl_setopt ( $ch, CURLOPT_URL, $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, $post_data ); curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:")); $return = curl_exec ( $ch ); curl_close ( $ch ); var_export($return);
访问地址:http://www.workerman.net/web-sender
推荐一个消息推送jquery查看:jGrowl 消息弹出框的jQuery插件 sdn加载:http://www.bootcdn.cn/jquery-jgrowl/
用法:
// Sample 1:提示后自动消息
$.jGrowl(
"Hello world!"
);
// Sample 2:须要用户手动关闭
$.jGrowl(
"Stick this!"
, { sticky:
true
});
// Sample 3:增长标题
$.jGrowl(
"A message with a header"
, { header:
'Important'
});
// Sample 4:指定在10秒后自动消失
$.jGrowl(
"A message that will live a little longer."
, { life: 10000 });
// Sample 5:消失后有回调事件
$.jGrowl(
"A message with a beforeOpen callback and a different opening animation."
, {
beforeClose:
function
(e,m) {
alert(
'About to close this notification!'
);
},
animateOpen: {
height:
'show'
}
});