域名转让:ai-php.cn wowphp.net
X
活动推荐

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

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

php swoole搭建简单的聊天室

查看:2362  回复: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

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

扫描二维码手机查看

发布评论:


登录:
  表情:
评论话题
推荐阅读:
霍金24岁博士毕业论文在线预览   阅读:4808什么是天使轮?什么是A轮融资?B轮融资?   阅读:3516centos7开启交换内存   阅读:9333用php从1加到100的值   阅读:7328centos 7 设置 nginx-1.11.10 开机启动   阅读:7089php中地址引用&的真正理解-变量引用、函数引用、对象引用   阅读:4826最简单的Banner轮播淡入淡出效果代码及实现思路(附带源码)   阅读:11949mysql 为什么添加索引可以提高访问速度   阅读:5214Mysql在大型网站的应用架构演变   阅读:3897浅谈javascript的函数节流   阅读:3973php设置cookie为HttpOnly防止XSS攻击   阅读:10865程序员找工作三要素(必看)   阅读:4947php压缩页面的html代码一行显示   阅读:6134最简单的Banner轮播淡入淡出效果代码及实现思路(附带源码)   阅读:11949爱编程,也爱健康   阅读:3530php打印三角形   阅读:6884纯干货,程序员面试的超实用技巧   阅读:3603mysql数据一键导出到csv文件   阅读:4773解决 nginx 413 request entity too large   阅读:3770Mysql在大型网站的应用架构演变   阅读:3897centos 7 设置 nginx-1.11.10 开机启动   阅读:7089javascript点击复制内容   阅读:5654linux php7安装yaf扩展   阅读:7009linux php7编译安装mongodb扩展   阅读:7240php打印九九加法表   阅读:8345面试都会问你为什么你从上一家公司离职的真实意思   阅读:9654PHP 鸟哥:我也曾经是“不适合”编程的人   阅读:12175centos 7 忘记root密码   阅读:4096php删除一个文件夹内的所有文件夹和文件   阅读:4139人性漫画:打工与创业的残酷区别   阅读:4001javascript对cookie操作详细代码函数   阅读:4647给 centos 7 安装桌面环境   阅读:15585linux教程,鸟哥私房菜(pdf全集)   阅读:7675centos7开启交换内存   阅读:9333最简单的Banner轮播淡入淡出效果代码及实现思路(附带源码)   阅读:11949linux top命令详解   阅读:7828centos 7 安装 samba 服务   阅读:5592mysql数据库性能的基本优化   阅读:3956面试都会问你为什么你从上一家公司离职的真实意思   阅读:9654知乎百万赞作者告诉你:年薪百万的人都在靠什么赚钱   阅读:5309php打印九九加法表   阅读:8345移动端js触摸touch详解(附带案例源码)   阅读:14467php打印三角形   阅读:6884mongodb设置shell开机启动脚本   阅读:8206企业让你去面试,可能不是真的招人   阅读:5143centos7中颁发CA证书并开启web https   阅读:5255ajax+php 实现一个简单的在线聊天室功能(附带源码)   阅读:7817最新centos7 搭建LNMP环境(centos7.2+php7+mysql5.7+nginx1.11+redis3.2)   阅读:9513shell脚本破解十位数内的所有纯数字rar加密压缩包脚本   阅读:7555centos 7 忘记root密码   阅读:4096