阅读模式:

php swoole搭建简单的聊天室

查看:4930  回复:1  类型:  来源:php自学网  标签 html5 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 = 'wss://zixuephp.net/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>&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":"'+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

    swoole服务端:

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

<?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":"☺php自学网","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'] = '☺思知-机器人';
            $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' => 'root',
        'password' => '123456',
        'database' => 'zixuephp',
    ]);

    $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'] = '☺php自学网-主动推送-推荐阅读';
                    $json['msg'] = "<a target=\"_blank\" 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';
        }

四、php运行服务端

    nohup /usr/bin/php server.php &

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

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

扫描二维码手机查看

最新评论:
Anakin  发表于 17天前 
沙发
回复
回复:[主题]
表情:
 提交评论
清空

发布评论:


登录:
  表情:
评论话题
推荐阅读:
给网站添加一键qq登录的功能   阅读:8034centos 7 设置 nginx-1.11.10 开机启动   阅读:8455最新centos7 搭建LNMP环境(centos7.2+php7+mysql5.7+nginx1.11+redis3.2)   阅读:11528nginx安装redis模块   阅读:6575程序员找工作三要素(必看)   阅读:6051php 获取当前前后年、月、星期、日、时分秒的时间   阅读:7245linux教程,鸟哥私房菜(pdf全集)   阅读:10740mysql数据一键导出到csv文件   阅读:6147php+redis实现消息队列   阅读:15802给网站添加一键qq登录的功能   阅读:8034爱编程,也爱健康   阅读:4738ajax+php 实现一个简单的在线聊天室功能(附带源码)   阅读:10221shell脚本批量删除几天前的文件   阅读:10888shell发送邮件脚本   阅读:9032centos 7 安装 samba 服务   阅读:7301把php session 会话保存到redis   阅读:7803解决 nginx 413 request entity too large   阅读:4859centos 7.2 添加php7 的 php-fpm 开机启动   阅读:25659linux php7编译安装mongodb扩展   阅读:9485php打印九九乘法表   阅读:11449网站局部小图片优化-base64编码图片   阅读:19929crontab+shell脚本实现定时备份mysql数据库   阅读:8184最简单的Banner轮播淡入淡出效果代码及实现思路(附带源码)   阅读:14014linux命令中执行php脚本   阅读:8710php连接redis   阅读:6065知乎百万赞作者告诉你:年薪百万的人都在靠什么赚钱   阅读:7222mysql 为什么添加索引可以提高访问速度   阅读:6468用php从1乘到100的值   阅读:6736最简单的Banner轮播左右切换效果代码及实现思路(附带源码)   阅读:32939centos 7 修改系统屏幕分辨率   阅读:47236linux php7安装yaf扩展   阅读:9080mysql数据库性能的基本优化   阅读:4838nginx编译安装后对nginx进行平滑升级   阅读:7196ajax+php 实现一个简单的在线聊天室功能(附带源码)   阅读:10221mysql数据库性能的基本优化   阅读:4838简单的DOS攻击之死亡之ping详解   阅读:62875centos7中颁发CA证书并开启web https   阅读:8694php显示刚刚、几分钟前、几小时前、几天前的函数   阅读:9161TCP的三次握手(建立连接)和四次挥手(关闭连接)   阅读:4221php连接redis   阅读:6065网站性能优化-页面静态缓存   阅读:5855html5 离线缓存的使用   阅读:4716mysql共享锁和排他锁详解   阅读:7915centos 7 安装 mysql-5.7   阅读:7683php swoole搭建简单的聊天室   阅读:4931把php session 会话保存到redis   阅读:7803centos 7 安装 samba 服务   阅读:7301php生成二维码   阅读:7350Nginx 配置文件详解   阅读:5322centos 7 设置 nginx-1.11.10 开机启动   阅读:8455