Workerman之GatewayWorker框架 - 在线客服,实时通信

2019-05-26 20:04:46 浏览数 (3)

一、绑定用户id实现一对一客服聊天

相关文档:LibGateway类提供的接口

JS:

代码语言:javascript复制
<script>

    var fromid = {$fromid};

    var toid = {$toid};

     var ws =  new WebSocket("ws://127.0.0.1:8282");

      ws.onmessage = function(e){

       var message =  eval("(" e.data ")");
          switch (message.type){
              case "init":
                  var bild = '{"type":"bind","fromid":"' fromid '"}';
                   ws.send(bild);
                  return;
              case "text":
                  $(".chat-content").append(' <div class="chat-text section-left flex"><span class="char-img" style="background-image: url(http://chat.com/static/newcj/img/123.jpg)"></span> <span class="text"><i class="icon icon-sanjiao4 t-32"></i>' message.data '</span> </div>');
                  return;
          }
    }

     $(".send-btn").click(function(){

         var text = $(".send-input").val();

         var message = '{"data":"' text '","type":"say","fromid":"' fromid '","toid":"' toid '"}';

         $(".chat-content").append('<div class="chat-text section-right flex"><span class="text"><i class="icon icon-sanjiao3 t-32"></i>' text '</span> <span class="char-img" style="background-image: url(http://chat.com/static/newcj/img/132.jpg)"></span> </div>');

         ws.send(message);

         $(".send-input").val("");
     })

</script>
代码语言:javascript复制
/**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        global $num;
        // 向当前client_id发送数据
        //Gateway::sendToClient($client_id, "Hello $client_idrn");
        // 向所有人发送
       // Gateway::sendToAll("$client_id loginrn");

        echo "connect".  $num.":".$client_id."n";

        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 = $message_data['fromid'];
               Gateway::bindUid($client_id, $fromid);
               return;
           case "say":

               $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()
               ];
                Gateway::sendToUid($toid, json_encode($date));
//               Gateway::sendToAll(json_encode($date));
               return;
       }
   }

Test:

二、数据持久化与优化两个聊天对象的唯一性

代码语言:javascript复制
   public static function onMessage($client_id, $message)
   {
       $message_data = json_decode($message,true);
       if(!$message_data){
           return;
       }
       switch($message_data['type']){
           case "bind":
               $fromid = $message_data['fromid'];
               Gateway::bindUid($client_id, $fromid);
               return;
           case "say":
               $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)){
                   Gateway::sendToUid($toid, json_encode($date));
                   $date['isread']= 1;
               }else{
                   $date['isread']=0;
               }

               $date['type']="save";
               Gateway::sendToUid($fromid,json_encode($date));

               return;
       }
   }
代码语言:javascript复制
ws.onmessage = function(e){

       var message =  eval("(" e.data ")");
          switch (message.type){
              case "init":
                   var bild = '{"type":"bind","fromid":"' fromid '"}';
                   ws.send(bild);

                  return;
              case "text":
                  //指定两个聊天对象聊天
                      if(toid==message.fromid) {

                          $(".chat-content").append(' <div class="chat-text section-left flex"> <span class="text"><i class="icon icon-sanjiao4 t-32"></i>'   message.data   '</span> </div>');
                      }
                  return;
              case "save":
                  //数据持久化
                  save_message(message);
                  return;
          }
    }

三、发图片、QQ表情、聊天列表、所有页面websocket,消息推送、实时更新

相关代码:https://github.com/tcyfree/ch...

参考教程:workerman实战之PHP在线客服

0 人点赞