PHP socket
Workerman是一款开源高性能异步PHP socket即时通讯框架。
非常好用的一款框架,可以支持在线聊天,长连接等
用法
官方 https://www.workerman.net/
1,在php端,下载好
php start.php start -d
命令启动
2、在html端
<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>
3、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 ); }
|
请发表评论