源碼 ASP源碼 PHP源碼 NET源碼 JAVA源碼 FLASH源碼 其它源碼 整站源碼 常見問題

Workerman之GatewayWorker框架 - 在線客服,實時通信

PHP源碼 HTML我幫您 1月前  次瀏覽

一、綁定用戶id實現一對一客服聊天

相關文檔:LibGateway類提供的接口

JS:


/**
     * 當客戶端連接時觸發
     * 如果業務不需此回調可以刪除onConnect
     * 
     * @param int $client_id 連接id
     */
    public static function onConnect($client_id)
    {
        global $num;
        // 向當前client_id發送數據
        //Gateway::sendToClient($client_id, "Hello $client_id\r\n");
        // 向所有人發送
       // Gateway::sendToAll("$client_id login\r\n");

        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:



二、數據持久化與優化兩個聊天對象的唯一性

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;
       }
   }

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(' 
' + message.data + '
'); } return; case "save": //數據持久化 save_message(message); return; } }

三、發圖片、QQ表情、聊天列表、所有頁面websocket,消息推送、實時更新

相關鏈接

發表評論

2017怎样手机上赚钱