X
活动推荐

阿里云高性能云主机2折293元/年

企业级性能云服务器限时2折起
新用户5折爆款服务器会场
查看详情 293元/年
阅读模式:

php swoole搭建简单的聊天室

查看:1552  回复:0  类型:  来源:php自学网  标签 php swoole

        php使用swoole扩展搭建简单的在线实时聊天室,附带源码和界面预览。前端页面使用html5 websocket。

        预览地址:http://zixuephp.net/chat.html

一、websocket

    客户端完整源码查看:view-source:http://zixuephp.net/chat.html

    websocket客户端:

//client.html
$(function(){    
var wsname = $('.wsname1').text();    
var wsimg = $('.chatheadimg').attr('src');    
var wsServer = 'ws://106.12.107.148:9501';    
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 = 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>&nbsp;&nbsp;\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>&nbsp;&nbsp;\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":"'+chatcon+'","self":"0","img":"'+wsimg+'"}');    
});    
window.onunload = function() {    
websocket.send('{"type":"3","name":"'+wsname+'","msg":"下线了!","self":"0","img":"'+wsimg+'"}');    
}    
});

二、php swoole

    swoole服务端:

    需要安装php swoole扩展才能运行。编译安装的php如果没有swoole扩展的安装方法:http://zixuephp.net/article-430.html

//server.php
//创建websocket服务器对象,监听0.0.0.0:9501端口
$ws = new swoole_websocket_server("0.0.0.0", 9501);

//监听WebSocket连接打开事件
$ws->on('open', function ($ws, $request) {
    //var_dump($request->fd);
    //var_dump($request->get);
    //var_dump($request->server);
    //$ws->push($request->fd, $request->fd);
});

//监听WebSocket消息事件
$ws->on('message', function ($ws, $frame) {
    $message = '';
    $message = $frame->data;
    $person = count($ws->connections);
    foreach($ws->connections as $fd){
        if($fd == $frame->fd){
            $message = json_decode($message,true);
            $message['self'] =1;
            $message['time'] =date('Y-m-d H:i:s',time());
            $message['person'] = $person;
            $message = json_encode($message);
            $ws->push($fd,$message);
        }else{
            $message = json_decode($message,true);
            $message['self'] =0;
            $message['time'] =date('Y-m-d H:i:s',time());
            $message['person'] = $person;
            $message = json_encode($message);
            $ws->push($fd, $message);
        }
    }
    //echo "message:".$message."\n";
});

//监听WebSocket连接关闭事件
$ws->on('close', function ($ws, $fd) {
    //echo "client-{$fd} is closed\n";
});

$ws->start();

三、php运行服务端

    nohup /usr/bin/php server.php &

[root@zixuephp /]# whereis php
php: /usr/bin/php /usr/local/php
[root@zixuephp /]# nohup /usr/bin/php server.php &
分享到:
0 0

*有问题之处烦请在评论中指出非常感谢!
不是我想要的内容,继续搜索:

扫描二维码手机查看

发布评论:


登录:
  表情:
评论话题
推荐阅读:
给网站添加一键新浪微博登录的功能   阅读:5418php+redis实现消息队列   阅读:10124linux命令中执行php脚本   阅读:5361centos 7 安装 mysql-5.7   阅读:4804centos7中颁发CA证书并开启web https   阅读:4028php打印九九乘法表   阅读:7191centos 7搭建zabbix3.4   阅读:4332web性能测试工具ab的测试方法   阅读:3671php swoole搭建简单的聊天室   阅读:1553web性能测试工具ab的测试方法   阅读:3671简单的DOS攻击之死亡之ping详解   阅读:34578栈和堆的区别   阅读:3835比phpexcel还要简单的excel CSV 一键导入数据到数据库   阅读:4028centos 7.2 添加php7 的 php-fpm 开机启动   阅读:18630php压缩页面的html代码一行显示   阅读:5580centos 7搭建zabbix3.4   阅读:4332centos7.3安装mongodb3.6   阅读:5526爱编程,也爱健康   阅读:3163纯干货,程序员面试的超实用技巧   阅读:3266最简单的Banner轮播左右切换效果代码及实现思路(附带源码)   阅读:25873企业让你去面试,可能不是真的招人   阅读:4772php文件下载防盗链   阅读:7885mysql数据一键导出到csv文件   阅读:4362网站局部小图片优化-base64编码图片   阅读:6429centos 7 开启网络   阅读:4684小米手机端商城rem适配原理   阅读:5664什么是天使轮?什么是A轮融资?B轮融资?   阅读:3109关于PHP程序员技术职业生涯规划   阅读:1611一键分享到QQ空间、QQ好友、新浪微博、微信代码   阅读:102950php冒泡排序法   阅读:10723csv文件一键导入数据到mysql数据库   阅读:5314最新centos7 搭建LNMP环境(centos7.2+php7+mysql5.7+nginx1.11+redis3.2)   阅读:8795移动端自适应rem原理   阅读:8130linux时间戳有趣的情人节秒1234567890   阅读:4280mysql5.7主从同步中binlog同步模式详解   阅读:3722php 统计网页打开耗时和脚本运行内存   阅读:5395crontab+shell脚本实现定时备份mysql数据库   阅读:5741mongodb设置shell开机启动脚本   阅读:7229php生成二维码   阅读:4325linux rarcrack破解rar、zip和7z压缩包简单密码的工具   阅读:11908PHP 鸟哥:我也曾经是“不适合”编程的人   阅读:10109php显示刚刚、几分钟前、几小时前、几天前的函数   阅读:6119移动端js触摸touch详解(附带案例源码)   阅读:13049phpexcel 超简单从excel表格一键导入数据到数据库教程   阅读:10128十张GIF让你弄懂递归等概念   阅读:4762php删除一个文件夹内的所有文件夹和文件   阅读:3831centos 7 修改系统屏幕分辨率   阅读:34658十张GIF让你弄懂递归等概念   阅读:4762mysql数据一键导出到csv文件   阅读:4362mysql 为什么添加索引可以提高访问速度   阅读:4807