2012-08-17 37 views
0

我使用Ajax-php-xml技術創建了聊天應用程序。如果用戶數量低於50,那麼它工作正常。否則,應用程序的速度急劇下降。我想一次管理200個用戶。如何提高Ajax-php-xml聊天應用程序的速度?

我的XML格式是

<?xml version="1.0" encoding="utf-8"?> 
     <messageData> 
     <message> 
     <sender>user__5338</sender> 
     <receiver>user__5339</receiver> 
     <content>hello</content> 
     <date_time>2012-08-17 09:24:57</date_time> 
     <status>unread</status> 
     </message> 
     <message> 
     <sender>user__5338</sender> 
     <receiver>user__5339</receiver> 
     <content>hello</content> 
     <date_time>2012-08-17 09:26:21</date_time> 
     <status>unread</status> 
     </message> 
     </messageData> 

在我的php,我用這個方法

$xml = simplexml_load_file($this->xml_file); 
    foreach($xml->message as $chat_data){ 
     if($chat_data->status =='unread' && $chat_data->receiver ==$username){ 
      $chat_data->status = 'read'; 
      $xml->asXML($this->xml_file); 
      $sender =(string)$chat_data->sender; 
      $chat['message'] = (string)$chat_data->content; 

      $chat['date'] = (string)$chat_data->date_time; 
     } 
    } 

如何聊天次數增加應用程序的速度獲取從XML文件中的數據?我可以通過使用節點js來提高速度嗎?

+2

AJAX是不是真的適合這一類的事情,至少最佳。 Web套接字的實現更復雜,但可能值得研究。他們是爲這種事情設計的。 – Utkanos 2012-08-17 10:40:56

回答

0

爲什麼不使用json作爲ajax傳輸數據類型?我相信它可能會更快,因爲PHP不需要解析xml。並使用jQuery作爲您的ajax請求的JavaScript庫,它是最好的之一。它支持json。並以this tutorial爲例。

還有一件事,將ajax請求之間的間隔設置爲1-3秒。也許更多。

0

使用套接字可以肯定會提高您的通信速度,但聽起來可能聽起來很複雜,但對於您的目的而言,這是答案。 套接字是:

的WebSocket規範定義了一個API建立的web瀏覽器和服務器之間的「套接字」 連接。簡而言之: 是客戶端和服務器之間的持久連接,並且兩個 各方都可以隨時開始發送數據。

簡單的例子:

var connection = new WebSocket('ws://html5rocks.websocket.org/echo', ['soap', 'xmpp']); 
// When the connection is open, send some data to the server 
connection.onopen = function() { 
    connection.send('Ping'); // Send the message 'Ping' to the server 
}; 

// Log errors 
connection.onerror = function (error) { 
    console.log('WebSocket Error ' + error); 
}; 

// Log messages from the server 
connection.onmessage = function (e) { 
    console.log('Server: ' + e.data); 
}; 

搜索會給你更多的例子