0

Ajax-php-xmlテクノロジーを使用してチャットアプリケーションを作成しました。ユーザー数が50未満の場合は正常に機能します。それ以外の場合は、アプリケーションの速度が大幅に低下します。一度に200人のユーザーを管理したい。

私の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ファイルからデータをフェッチします

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

チャットアプリケーションの速度を上げるにはどうすればよいですか?ノードjsを使用して速度を上げることはできますか?

4

2 に答える 2

0

json を ajax 転送データ型として使用しないのはなぜですか? PHPはxmlを解析する必要がないため、より高速になる可能性があると思います。また、ajax リクエストの JavaScript ライブラリとして jQuery を使用すると、最高のライブラリの 1 つになります。json に対応しています。このチュートリアルを例として使用してください。

もう 1 つ、ajax リクエストの間隔を 1 ~ 3 秒に設定します。多分もっと。

于 2012-08-17T10:42:59.437 に答える
0

ソケットを使用すると、確かに通信速度が向上する可能性があります。複雑に聞こえるかもしれませんが、目的にはそれが答えです。ソケットは次のとおりです。

WebSocket 仕様は、Web ブラウザーとサーバー間の「ソケット」接続を確立する API を定義します。簡単に言えば、クライアントとサーバーの間に永続的な接続があり、両者はいつでもデータの送信を開始できます。

簡単な例:

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

検索すると、より多くの例が得られます

于 2012-08-17T10:48:27.847 に答える