對不起的男孩,但我不明白的地方你會發現這個問題......這真的很簡單
案例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觸發器! =)