workerman实现的web在线聊天

项目介绍

项目是基于Workerman的GatewayWorker框架,GatewayWorker是用于快速开发TCP长连接应用,此项目是基于GatewayWorker实现的在线即时聊天系统。

系统功能

1.文字发送 2.QQ表情发送 3.图片发送 4.聊天记录持久化 5.长连接下即时展示对方在线状态 6.一对一聊天 7.消息群发(代码中注释,可自行调试)8.消息读取状态以及未读条数显示。

演示

  

截图1地址链接

截图2地址链接

截图3地址链接

前端核心代码

Url: your app/application/index/view/index/index.html

<!doctype html>
<html>
<body>
...
<script>
     var ws =  new WebSocket("ws://Your Ip:8282");    // 与gatewayworker建立链接
      ws.onmessage = function(e){
          switch (message.type){                 // 链接成功,返回初始化数据 
              case "init":                       
                   var bild = 'user id';
                   ws.send(bild);                // user Id与gatewayworker的client id绑定
                   return;
              case "text":
                  console.log(message.data);     // 推送过来的消息
                  return;
              case "say_img":
                  console.log(message.img_name); // 推送过来的图片              
                  return;
              case "save":
                  console.log(message.isread);   // 推送对方是否读取消息                     
                  return;
              case  "online":
                  console.log(message.status);   // 推送对方在线状态
                  return;
              case "onclose":
                  console.log(message.uid);       // 推送对方下线通知
                  return;

          }
    }
</script>
</body>
</html>

后端核心代码

Url:your app/vendor/GatewayWorker-for-win/Applications/YourApp/Events.php

<?php

use \GatewayWorker\Lib\Gateway;

/**
 * 主要是处理 onConnect onMessage onClose 三个方法
 */
class Events
{
    /**
     * 当客户端连接时触发
     *
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        global $num;
        Gateway::sendToClient($client_id,json_encode([
           'type'=>'init',
           'client_id'=>$client_id
       ]));

    }

   /**
    * 当客户端发来消息时触发
    * @param int $client_id 连接id
    * @param mixed $message 具体消息
    */
   public static function onMessage($client_id, $message)
   {
       $message_data = json_decode($message,true);
       if(!$message_data){
           return;
       }
       switch($message_data['type']){
           case "bind": // fromid(userId)和gatewayworker的client_id绑定
               $fromid = $message_data['fromid'];
               Gateway::bindUid($client_id, $fromid);
               return;
           case "say": // 消息推送给指定id
               $text = nl2br(htmlspecialchars($message_data['data']));
               $fromid = $message_data['fromid'];
               $toid = $message_data['toid'];
               $date=[
                   'type'=>'text',
                   'data'=>$text,
                   'fromid'=>$fromid,
                   'toid'=>$toid,
                   'time'=>time()
               ];
               if(Gateway::isUidOnline($toid)){
                   $date['isread']= 1;
                   Gateway::sendToUid($toid, json_encode($date));   
               }else{
                   $date['isread']=0;
               }
               $date['type']="save";
               Gateway::sendToUid($fromid,json_encode($date));      // 消息推送给本机,并做数据持久化
               return;
           case "say_img": // 图片推送给指定id
               $toid = $message_data['toid'];
               $fromid =$message_data['fromid'];
               $img_name = $message_data['data'];
               $date=[
                   'type'=>'say_img',
                   'fromid'=>$fromid,
                   'toid'=>$toid,
                   'img_name'=>$img_name
               ];
               Gateway::sendToUid($toid,json_encode($date));
               return;
           case "online": // 推送对方在线状态
               $toid = $message_data['toid'];
               $fromid = $message_data['fromid'];
               $toidStatus = Gateway::isUidOnline($toid);
               $fromidStatus = Gateway::isUidOnline($fromid);
               $toid_client_id = Gateway::getClientIdByUid($toid);
               $fromid_client_id = Gateway::getClientIdByUid($fromid);
               Gateway::sendToUid($fromid,json_encode(['type'=>"online","status"=>$toidStatus
                   ,"toid_client_id"=>$toid_client_id[0],"fromid_client_id"=>$fromid_client_id[0]]));
               Gateway::sendToUid($toid,json_encode(['type'=>"online","status"=>$fromidStatus
                   ,"toid_client_id"=>$fromid_client_id[0],"fromid_client_id"=>$toid_client_id[0]]));
               return;
       }
   }

   /**
    * 当用户断开连接时触发
    * @param int $client_id 连接id
    */
   public static function onClose($client_id)
   {
        GateWay::sendToAll(json_encode(['type'=>"onclose","uid"=>$client_id]));
   }
}

下载

$ git clone https://gitee.com/kitukeyii/chatonline-gatewayworker.git

项目地址

https://gitee.com/kitukeyii/chatonline-gatewayworker

项目组件

socket框架:GatewayWorker 3.0.7

后台框架:Thinkphp 5.4

基础依赖:php5.6 mysql5.7

安装指南

1.启动wokerman

cd vendor/GatewayWorker-for-win

Linux (以daemon(守护进程)方式启动) :

 php start.php start -d 

Win:

   双击 start_for_win.bat 

2.部署php项目

 Apache:

<VirtualHost *:80>
	ServerName chat.com
	ServerAlias chat.com
	DocumentRoot Your Dir\chatonline-gatewayworker\public
	<Directory  Your Dir\chatonline-gatewayworker\public>
		Options +Indexes +Includes +FollowSymLinks +MultiViews
		AllowOverride All
		Require local
	</Directory>
</VirtualHost>

Nginx:

server
{
    listen 80;
    server_name your server_name;
    index index.php index.html index.htm default.php default.htm default.html;
    root Your Dir/chatonline-gatewayworker/public;
    ...
}

完成!

猜你喜欢

转载自blog.csdn.net/qq_24973351/article/details/111350826
今日推荐