PHP socketphp
Workerman是一款開源高性能異步PHP socket即時通信框架。css
很是好用的一款框架,能夠支持在線聊天,長鏈接等html
用法jquery
官方 https://www.workerman.net/web
1,在php端,下載好後端
php start.php start -dapi
命令啓動服務器
二、在html端app
<script src='/static/js/jquery-3.2.1.min.js'></script>
<script src='http://cdn.bootcss.com/socket.io/1.3.7/socket.io.js'></script>
<script>
// 鏈接服務端,workerman.net:2120換成實際部署web-msg-sender服務的域名或者ip
var socket = io('http://localhost:2120');
// uid能夠是本身網站的用戶id,以便針對uid推送以及統計在線人數
uid = '{$user.id}';
console.log(uid)
// socket鏈接後以uid登陸
socket.on('connect', function(){
socket.emit('login', uid);
});
// 後端推送來消息時
socket.on('new_msg', function(msg){
console.log(msg)
$('.man').append(msg)
// var btn = $(".label-danger-msg").text();
//$(".label-danger-msg").html(btn*1+1)
});
// 後端推送來在線數據時
/*socket.on('update_online_count', function(online_stat){
console.log(online_stat);
});*/框架
</script>
三、php端
function sendall($touid,$txt){
// 推送的url地址,使用本身的服務器地址 $push_api_url = "http://localhost:2121/"; $post_data = array( "type" => "publish", "content" => $txt, "to" => $touid, ); $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 ); }