2017-10-15 43 views
0

我與網絡插口存儲消息在MySQL數據庫中PHP的知道,如果一個用戶已經閱讀發來的消息,也讓所有未讀郵件

下一個實時聊天,是我的數據庫結構

tbl_messages

id 
message 
sent_by 
sent_on 

爲是的情況下,用戶有用的關係的另一個表中刪除一個消息只刪除他的關係

tbl_message_users

message_id 
user_id 
read // 0 or 1 

上述缺省值爲0(未讀出)該用戶的read到哪些消息被髮送到。

現在,在用戶點擊發送按鈕後,我的網絡套接字中,發送消息事件在nodejs中被觸發,它執行一個函數來再次檢索消息以進行更新。

什麼不確定如何實現未讀消息。假設用戶處於離線狀態,我希望他得到未讀消息的通知,這些消息是read=0的消息。

假設用戶已登錄但尚未打開聊天框,該聊天框也會成爲未讀消息。

我該如何解決這個問題?

回答

-2

對不起的男孩,但我不明白的地方你會發現這個問題......這真的很簡單


案例1:離線用戶通知(服務器端)

當用戶登錄進入聊天服務您需要在「tbl_messages_users」中使用參數「read = 0」進行查詢。然後,你必須樣式化DB的輸出

/** I'm using PHP syntax because I'm not good with NodeJS **/ 

$query = new Query(...); 
$results = $query->getResults(); 
if(count($results) > 0) { 
    foreach($results as $result) { 
     /** .... STYLISH THIS (Ex. in HTML) .... **/ 
    } 
} 

如何使用招?當服務器自動生成用戶(服務器端)頁面


案例2:在線用戶(客戶端-Sider的)

/** I'm using jQuery syntax **/ 

/** 
* %HTTP_METHOD%     : GET or POST ? 
* %SERVER_SIDE_URL%    : Where is the action? 
* %DATA_TO_SERVER_SIDE_ACTION% : Example ClientID 
* 
* We use JSON Data Object because is really simple to parse in jQuery 
**/ 
$.ajax({ 
    type: "%HTTP_METHOD%", 
    url: "%SERVER_SIDE_URL%", 
    data: "%DATA_TO_SERVER_SIDE_ACTION%" 
}).done(function(json) { 
    /** indexOf prevent invalid HTML characters **/ 
    json = json.substring(json.indexOf("{")); 
    json = $.parseJSON(json); 

    /** Create Your JSON Data Structure, iterate It and generate HTML to append in notification panel **/ 

}).fail(function(httpResponse) { 
    /** 
    * Stylish an HTML exception 
    * httpResponse : Contains Server HTTP response data (Ex. response code like 200, ..., 500) 
    **/ 
}); 

如何調用Ajax請求?

setTimeout(function() { 
    myAjaxRequest(...params): 
}, %EVERY_X_MILLISECONDS%); 

或使用觸發

$(document).on("%MY_TRIGGER%", myCallbackFunction); 

function myCallbackFunction(e) { 
    myAjaxRequest(...params): 
} 

如何使用招?當用戶調用服務器API(客戶端)


與案例2

結合案例1在%SERVER_SIDE_URL%(案例2)你可以把返回JSON數據網址對象

/** I'm using PHP syntax because I'm not good with NodeJS **/ 

$query = new Query(...); 
$results = $query->getResults(); 
$RESULT = array(); 
if(count($results) > 0) { 
    $RESULT['status'] = 'OK'; 
    $RESULT['unreadMessages'] = array(); 
    foreach($results as $result) { 
     $RESULT['unreadMessages'][] = array(
      'message' => $result->message, 
      'date' => $result->messageDate, 
      /** ... etc **/ 
     ); 
    } 
} else { 
    $RESULT = array(
     'status' => 'KO', 
     'error' => 'No unread messages for user %USER_ID%', 
    ); 
} 

echo json_encode($RESULT); 
exit; /** PREVENT SCRIPT TO REMAIN ALIVE IN BACKGROUND **/ 

呼叫通過Ajax請求的服務器頁面(在個案2見)


結論

你可以用第2種情況,當用戶登錄到聊天系統,或者用戶也進入聊天系統。當你想要時啓動jQuery觸發器! =)

相關問題