田野里,高粱像喝醉了酒,频频点头;玉米正在变黄了的衣服里睡大觉;大豆也小坡了肚皮,蹦了出来;小白菜像列队的士兵整齐地排列在在菜地里。农民正忙忙碌碌地收获着一年的成果,田野里不时传出阵阵欢笑声。啊,秋天的景色真美啊!
php使用swoole扩展搭建简单的在线实时聊天室,附带源码和界面预览。前端页面使用html5 websocket。
一、websocket
//client.html
$(function(){
var wsname = $('.wsname1').text();
var wsimg = $('.chatheadimg').attr('src');
var wsServer = 'wss://www.ytymz.com/ws';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (event) {
layer.msg('聊天室连接成功!');
websocket.send('{"type":"1","name":"'+wsname+'","msg":"上线了!","self":"0","img":"'+wsimg+'"}');
};
websocket.onclose = function (event) {
//console.log("连接关闭!");
websocket.send('{"type":"3","name":"'+wsname+'","msg":"下线了!","self":"0","img":"'+wsimg+'"}');
};
websocket.onmessage = function (event) {
//console.log(event.data);
var chatmessage = JSON.parse(event.data);
if(chatmessage){
var ctype = chatmessage['type'];
var cself = chatmessage['self'];
var cname = chatmessage['name'];
var ctime = chatmessage['time'];
var cmsg = decodeURIComponent(chatmessage['msg']);
var cimg = chatmessage['img'];
var person = chatmessage['person'];
if(ctype == 1){
if( cself== 0){
layer.msg('【'+cname+'】'+cmsg);
}
$('.persion1').text(person);
}else if (ctype == 2){
var html ='';
if( cself == 1){
html = "<div class=\"clearfix chatmessage\">\n" +
" <div class=\"chatnametime rrn\">\n" +
" <span>"+cname+"</span> \n" +
" <span>"+ctime+"</span>\n" +
" </div>\n" +
" <div class=\"chatimgcon\">\n" +
" <div class=\"fr\">\n" +
" <img src=\""+cimg+"\" />" +
" </div>\n" +
" <div class=\"fr chcon\">"+cmsg+"</div>\n" +
" </div>\n" +
" </div>";
}else{
html = "<div class=\"clearfix chatmessage\">\n" +
" <div class=\"chatnametime\">\n" +
" <span>"+cname+"</span> \n" +
" <span>"+ctime+"</span>\n" +
" </div>\n" +
" <div class=\"chatimgcon\">\n" +
" <div class=\"fl\">\n" +
" <img src=\""+cimg+"\" />\n" +
" </div>\n" +
" <div class=\"fl chcon\">"+cmsg+"</div>\n" +
" </div>\n" +
" </div>";
}
$('.chatdetails').append(html);
$('.persion1').text(person);
$(".chatdetails").animate({scrollTop:$('.chatdetails')[0].scrollHeight},200);
}else if(ctype == 3){
$('.persion1').text(person);
layer.msg('【'+cname+'】'+cmsg);
}
}
};
websocket.onerror = function (event, e) {
layer.msg('聊天室错误'+event.data);
};
$(".chatsend").click(function(){
var chatcon = '';
chatcon = $('.chatcontent textarea').val();
websocket.send('{"type":"2","name":"'+wsname+'","msg":"'+encodeURIComponent(chatcon)+'","self":"0","img":"'+wsimg+'"}');
$('.chatcontent textarea').val(' ');
$('.chatcontent textarea').focus();
});
window.onunload = function() {
websocket.send('{"type":"3","name":"'+wsname+'","msg":"下线了!","self":"0","img":"'+wsimg+'"}');
}
//心跳
setTimeout(function(){
setInterval(function(){
websocket.send('{"type":"4","name":"","self":"","img":"');
},35000);
},5000);
});
二、PHP Swoole
<?php
//server.php
//创建websocket服务器对象,监听0.0.0.0:9501端口
$ws = new swoole_websocket_server("0.0.0.0", 9501);
$ws->set([
'daemonize' => true,
'worker_num' => 1,
'heartbeat_check_interval' => 60, //每60s检查一次心跳,遍历一次所有连接
'heartbeat_idle_time' => 120, //在这个时间内没有向服务器发送任何数据,此连接将被强制关闭
'log_file' => '/usr/local/openresty/nginx/html/swoole.log'
]);
//连接用户名映射
$ws->personList = [];
//mysql连接
$ws->mysql = null;
//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
//var_dump($request->fd);
//var_dump($request->get);
//var_dump($request->server);
$ws->push($request->fd, '{"type":"2","name":"☺haodaima.comBlog","msg":"哈喽~","self":"0","img":"/static/images/visitor.png","time":"-_-"}');
});
//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
$message = null;
$message = $frame->data;
$person = count($ws->connections);
file_put_contents('/usr/local/openresty/nginx/html/swoole_chat.log', date("Y-m-d H:i:s",time()) . rawurldecode($message) . "\r\n", FILE_APPEND); //聊天内容记录
$message = json_decode($message, true);
$message['self'] = 0;
$message['person'] = $person;
$message['time'] = date('Y-m-d H:i:s', time());
$message['person_list'] = $ws->personList;
//记录连接和人名
if (isset($message['name']) && !empty($message['name'])) {
$ws->personList[$frame->fd] = $message['name'];
}
//下线处理
if (isset($message['type']) && $message['type'] == 3) {
if (isset($ws->personList[$frame->fd])) {
unset($ws->personList[$frame->fd]);
}
}
//发送聊天消息
foreach ($ws->connections as $fd) {
$message['person_list'] = $ws->personList;
//返回心跳数据
if (isset($message['type']) && $message['type'] == 4) {
$ws->push($frame->fd, json_encode($message));
break;
}
if ($fd == $frame->fd) {
$message['self'] = 1; //自己
$ws->push($fd, json_encode($message));
} else {
$message['self'] = 0;
$ws->push($fd, json_encode($message));
}
}
//发送机器人消息
$msg = null; //机器人1
if (isset($message['msg']) && $message['type'] == 2) {
$rebotMsg = file_get_contents('http://api.qingyunke.com/api.php?key=free&appid=0&msg=' . trim(urldecode($message['msg'])));
if (!empty($rebotMsg)) {
$chatCon = json_decode($rebotMsg, true);
if ($chatCon['result'] == 0) {
$msg = $chatCon['content'];
}
}
}
if (!empty($msg)) {
foreach ($ws->connections as $fd) {
$message['self'] = 0;
$message['msg'] = $msg;
$message['name'] = '☺菲菲-机器人';
$messageJson = json_encode($message);
$ws->push($fd, $messageJson);
}
}
$msg = null; //机器人2
if (isset($message['msg']) && $message['type'] == 2) {
$rebotMsg = file_get_contents('http://www.tuling123.com/openapi/api?key=ffeabab48272433db2cdb9dd17f8bc91&info=' . trim(urldecode($message['msg'])));
if (!empty($rebotMsg)) {
$chatCon = json_decode($rebotMsg, true);
if ($chatCon['code'] == 100000) {
$msg = $chatCon['text'];
}
}
}
if (!empty($msg)) {
foreach ($ws->connections as $fd) {
$message['self'] = 0;
$message['msg'] = $msg;
$message['name'] = '☺图灵-机器人';
$messageJson = json_encode($message);
$ws->push($fd, $messageJson);
}
}
$msg = null;//机器人3
if (isset($message['msg']) && $message['type'] == 2) {
$rebotMsg = file_get_contents('https://api.ownthink.com/bot?spoken=' . trim(urldecode($message['msg'])));
if (!empty($rebotMsg)) {
$chatCon = json_decode($rebotMsg, true);
if ($chatCon['message'] == 'success') {
$msg = $chatCon['data']['info']['text'];
}
}
}
if (!empty($msg)) {
foreach ($ws->connections as $fd) {
$message['self'] = 0;
$message['msg'] = $msg;
$message['name'] = '☺haodaima.com-机器人';
$messageJson = json_encode($message);
$ws->push($fd, $messageJson);
}
}
});
//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
//echo "client-{$fd} is closed\n";
if (isset($ws->personList[$fd])) {
unset($ws->personList[$fd]);
}
});
//mysql连接
function mysql_conn($ws)
{
$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect([
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'huajiao238',
'password' => 'www.ytymz.com',
'database' => 'huajiao238',
]);
$ws->mysql = $swoole_mysql;
}
$ws->on('WorkerStart', function ($ws, $worker_id) {
//防止重复执行
if ($worker_id == 0) {
mysql_conn($ws);
//定时器
swoole_timer_tick(60000, function () use ($ws) {
$res = $ws->mysql->query("SELECT id,title,seo FROM post ORDER BY RAND() LIMIT 1");
if (!$res) {
//mysql断线重连
mysql_conn($ws);
} else {
foreach ($ws->connections as $fd) {
$json['type'] = 2;
$json['name'] = '☺haodaima.comBlog-主动推送-推荐阅读';
$json['msg'] = "<a target=\"_blank\" rel="nofollow" href=\"https://zixuephp.net/article-" . $res[0]['id'] . ".html\"><h3>" . $res[0]['title'] . "</h3>" . "<p>" . $res[0]['seo'] . "</p></a>";
$json['self'] = 0;
$json['img'] = "/static/images/push.png";
$json['person'] = count($ws->connections);
$json['time'] = date('Y-m-d H:i:s', time());
$ws->push($fd, json_encode($json));
}
}
});
}
});
$ws->start();
三、nginx配置代理转发
location /ws {
proxy_pass http://127.0.0.1:9501;
proxy_read_timeout 60s;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'Upgrade';
}
四、运行服务端
[root@huajiao238/]# nohup /usr/bin/php server.php &
到此这篇关于PHP swoole搭建简单的聊天室就介绍到这了。一个人生活中的失意乃至失败,在很大程度上来说,是心理的而不是现实的,是技术性的而不是宿命的。现实至多为失败者提供了解脱的借口,而对宿命的信仰最终无情地扑灭了成功的任何希望。更多相关PHP swoole搭建简单的聊天室内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!