GatewayWorker windows版做简单聊天PHP代码

GatewayWorker windows版做简单聊天PHP代码

  GatewayWorker手册https://www.kancloud.cn/walkor/gateway-worker/326105网站介绍很详细;

     GatewayWorker基于Workerman开发的一个项目框架,用于快速开发TCP长连接应用,例如app推送服务端、即时IM服务端、游戏服务端、物联网、智能家居等等

     GatewayWorker使用经典的Gateway和Worker进程模型。Gateway进程负责维持客户端连接,并转发客户端的数据给BusinessWorker进程处理,BusinessWorker进程负责处理实际的业务逻辑(默认调用Events.php处理业务),并将结果推送给对应的客户端。Gateway服务和BusinessWorker服务可以分开部署在不同的服务器上,实现分布式集群。

     GatewayWorker提供非常方便的API,可以全局广播数据、可以向某个群体广播数据、也可以向某个特定客户端推送数据。配合Workerman的定时器,也可以定时推送数据。

Workerman可以看做是一个纯粹的socket类库,可以开发几乎所有的网络应用,不管是TCP的还是UDP的,长连接的还是短连接的。Workerman代码精简,功能强大,使用灵活,能够快速开发出各种网络应用。同时Workerman相比GatewayWorker也更底层,需要开发者有一定的多进程编程经验。

因为绝大多数开发者的目标是基于Workerman开发TCP长连接应用,而长连接应用服务端有很多共同之处,例如它们有相同的进程模型以及单发、群发、广播等接口需求。所以才有了GatewayWorker框架,GatewayWorker是基于Workerman开发的一个TCP长连接框架,实现了单发、群送、广播等长连接必用的接口。GatewayWorker框架实现了Gateway Worker进程模型,天然支持分布式多服务器部署,扩容缩容非常方便,能够应对海量并发连接。可以说GatewayWorker是基于Workerman实现的一个更完善的专门用于实现TCP长连接的项目框架。介绍这么多,你也大致了解GatewayWorker是什么了,我就在我的项目小程序上使用它做了一个聊天功能,这里都是我自己写的代码;

GatewayWorker windows版做简单聊天PHP代码_第1张图片GatewayWorker windows版做简单聊天PHP代码_第2张图片GatewayWorker windows版做简单聊天PHP代码_第3张图片

上面简单的聊天功能,是利用GatewayWorker长连接来做。目前运行很稳定,所以拿出来分享一下。因为关于Workerman的长连接网上资料还是很少的;下面就是我的代码展示;

Events.php文件里的消息接收和推送,这里是PHP逻辑的处理地方

/**
 * 当客户端发来消息时触发
 * @param int $client_id 连接id
 * @param mixed $message 具体消息
 */
public static function onMessage($client_id, $message)
{
    $req_data = json_decode($message,true);
    if(isset($req_data['openid'])) {
        // 初始化db连接
        $mysqli = new mysqli("localhost", "*********", "*********", "paimai");
        if ($mysqli->connect_errno) {
            $arr = "链接数据库失败";
            $req_data['message'] = json_encode($arr);
            Gateway::sendToClient($client_id, $req_data['message']);
            exit();
        }
        $mysqli->set_charset('utf8');
            /***************************聊天部分***********************$req_data['status1'] ==2*************************************************************/
            //聊天
            if($req_data['openid_other']) {
                $openid = $req_data['openid'];
                $sql = "SELECT id,avater,username,openid FROM wemall_user WHERE openid='$openid' LIMIT 0,1";
                $openidList = $mysqli->query($sql);
                $openidLists = $openidList->fetch_assoc();
                $user_id = $openidLists['id'];//发送user_id;
                $openid_other = $req_data['openid_other'];
                $sql = "SELECT id,avater,username,openid FROM wemall_user WHERE openid='$openid_other' LIMIT 0,1";
                $openid_otherList = $mysqli->query($sql);
                $openid_otherLists = $openid_otherList->fetch_assoc();
                $other_user_id = $openid_otherLists['id'];//接收user_id;
                //把数据改为已读
                $sql = "UPDATE wemall_shop_chat SET is_read=1 WHERE send_user_id='$other_user_id' and receive_user_id='$user_id' and is_read='2' ";
                $res2 = $mysqli->query($sql);
                if (!$res2) {
                    $arrs = "储存更新失败";
                    $req_data['message'] = json_encode($arrs);
                    Gateway::sendToGroup($req_data['groups'], $req_data['message']);
                    exit();
                }
                //保存信息
                if ($req_data['message']) {
                    $content = urlencode($req_data['message']);
                    $group = $req_data['groups'];
                    $ctime = time();
                    $sql = "INSERT INTO wemall_shop_chat (send_user_id,content,receive_user_id,ctime,groups) VALUES ('$user_id','$content','$other_user_id','$ctime','$group')";
                    $res1 = $mysqli->query($sql);
                    if (!$res1) {
                        $arr = "储存数据失败";
                        $req_data['message'] = json_encode($arr);
                        Gateway::sendToGroup($req_data['groups'], $req_data['message']);
                        exit();
                    }
                }
                $sql = "SELECT * FROM wemall_shop_chat WHERE send_user_id='$user_id' and receive_user_id='$other_user_id' OR send_user_id='$other_user_id' and receive_user_id='$user_id' order by ctime asc";
                $shop_chat = $mysqli->query($sql);
                $arrList = array();
                while ($shop_chats = $shop_chat->fetch_assoc()) {
                    $shop_chats['content'] = urldecode($shop_chats['content']);//发送user_id;
                    $idd = $shop_chats['send_user_id'];
                    $sql = "SELECT avater,username,openid,remak_avater FROM wemall_user WHERE id='$idd' LIMIT 0,1";
                    $openidListg = $mysqli->query($sql);
                    $openidListgs = $openidListg->fetch_assoc();
                    $shop_chats['openid'] = $openidListgs['openid'];//发送user_id;
                    $shop_chats['username'] = urldecode($openidListgs['username']);//发送者昵称;
                    if($openidListgs['remak_avater']){
                        $shop_chats['avater'] = $openidListgs['remak_avater'];//发送者头像;
                    }else{
                        $shop_chats['avater'] = $openidListgs['avater'];//发送者头像;
                    }
                    $idd_other = $shop_chats['receive_user_id'];
                    $sql = "SELECT avater,username,openid,remak_avater FROM wemall_user WHERE id='$idd_other' LIMIT 0,1";
                    $openidListo = $mysqli->query($sql);
                    $openidListos = $openidListo->fetch_assoc();
                    $shop_chats['other_openid'] = $openidListos['openid'];//发送user_id;
                    $shop_chats['other_username'] = urldecode($openidListos['username']);//发送者昵称;
                    if($openidListos['remak_avater']){
                        $shop_chats['other_avater'] = $openidListos['remak_avater'];//发送者头像;
                    }else{
                        $shop_chats['other_avater'] = $openidListos['avater'];//发送者头像;
                    }
                    $arrList[] = $shop_chats;
                }
                $data['content_list'] = $arrList;
                $req_data['message'] = json_encode($data);
                // 将client_id加入某个组
                Gateway::joinGroup($client_id, $req_data['groups']);
                // 向某个分组的所有在线client_id发送数据
                Gateway::sendToGroup($req_data['groups'], $req_data['message']);
            }else{
                $arrd = "数据有误";
                $req_data['message'] = json_encode($arrd);
                Gateway::sendToClient($client_id, $req_data['message']);
            }
        }
}
心跳在文件start_gateway.php里面
// 心跳间隔
$gateway->pingInterval = 30;
// 心跳数据
$gateway->pingData = '{"type":"ping"}';
前端访问的链接和GatewayWorker配置这需要大家看手册了,不难的,我当前Windows反向代理用的是Linux来实现的
我就暂时就写到这里,有不懂得,可以留言,我们一起交流,一起进步,,写的不好的地方可以留言,谢谢!

你可能感兴趣的:(GatewayWorker windows版做简单聊天PHP代码)