あなただけにして、サーバーとエージェントのステータスをチェックするクライアント上のタイマーにAJAX呼び出しを追加する必要が
...
//エージェント側..
function checkStatus(){
jQuery.get('/server-url?agent_id=32&status', function(data){
if (data.status == 'typing')
$('#typing').html('The user/client is typing a message..');
else
$('#typing').html('');
checkStatus();
});
}
// Start the function begining.
setTimeout(checkStatus, 300);
var agent_timer;
$('#chatMessage').keyup(function(){
if (agent_timer)
clearTimeout(agent_timer);
if ($("#chatMessage").val() == ''){
status = 'empty';
} else {
status = 'writing';
}
agent_timer = setTimeout(function(){
// Send status to server php script
$.post('/server-url?agent_id=23', {status: status}, function(data){
// handle response
});
}, 400);
});
//サーバー側...
// check for agent, and other things...
if (isset($_GET['agent_id'])){
// Agent server side
if (isset($_POST['status']){
// Save status to database
$this->save(array('agent' => $agent, 'chat_id' => $chat_id, 'status' => $_POST['status']));
}
if (isset($_GET['status'])){
$data = $this->read(array('agent_id' => $chat_id));
}
} else {
// This is the client server side
if (isset($_GET['status'])) {
$data = $this->read(array('chat_id' => $chat_id));
return $data['status'];
}
}
// handle other things and return data if necessary
// echo json_encode($data);
//クライアント側JS
function checkStatus(){
jQuery.get('/server-url?chat_id=32&status', function(data){
if (data.status == 'typing')
$('#typing').html('The agent is typing a message..');
else
$('#typing').html('');
checkStatus();
});
}
// Start the function at begining.
setTimeout(checkStatus, 300);
// We can do the same, so the agent can know if user is typing too (you must also copy the above function)
var client_timer;
$('#chatMessage').keyup(function(){
if (client_timer) clearTimeout(client_timer);
if ($("#chatMessage").val() == ''){
status = 'empty';
} else {
status = 'writing';
}
client_timer = setTimeout(function(){
// Send status to server php script
$.post('/server-url?chat_id=23', {status: status}, function(data){
// handle response
});
}, 400);
});
は多分MySQLでの行を更新するよりも良い方法があります...しかし、私は今1を考えることはできません。..
1 :)ありがとう!私は実際にそのようなmysqlの行を更新することを考えましたが、サーバに負荷がかかりすぎるかどうかは分かりませんでしたが、そうではないと思います。 –
1つの質問、私はタイマーを作るために何をする必要がありますので、各キーストロークのPHP呼び出しではありません? –
スクリプトにsetTimeout関数とclearTimeout関数を追加しました。それをチェックしてください;) – elboletaire