ryanhowdy Posted April 19, 2007 Share Posted April 19, 2007 i'm using a basic chat script and i'm trying to add a section for users currently logged in. i only want to display the users who have posted in the last 2 min getUsersOnline() and handleReceiveUsers() are the new info that doesn't seem to work. i know each of them are being called correctly, i can output from either function, but it looks like handleReceiveUsers() doesn't receive the xml data correctly I can't figure out what's wrong... chat.php var sendReq = getXmlHttpRequestObject(); var receiveReq = getXmlHttpRequestObject(); var receiveReq2 = getXmlHttpRequestObject(); var lastMessage = 0; var mTimer; function startChat() { document.getElementById('txt_message').focus(); getChatText(); getUsersOnline(); } function getXmlHttpRequestObject() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { document.getElementById('p_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.'; } } function getUsersOnline() { if (receiveReq2.readyState == 4 || receiveReq2.readyState == 0) { receiveReq2.open("GET", 'getChat.php?chat=1&last=' + lastMessage, true); receiveReq2.onreadystatechange = handleReceiveUsers; var param = 'action=getusers'; receiveReq2.send(param); } } function handleReceiveUsers() { if (receiveReq2.readyState == 4) { var users_div = document.getElementById('div_users'); var xmldoc = receiveReq2.responseXML; var online_nodes = xmldoc.getElementsByTagName("online"); var n_online = online_nodes.length for (i = 0; i < n_online; i++) { var user_node = online_nodes[i].getElementsByTagName("user"); users_div.innerHTML += '<b>' + user_node[0].firstChild.nodeValue + '<b/><br/>'; } mTimer = setTimeout('getChatText();',2000); } } getChat.php <?php header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" ); header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" ); header("Cache-Control: no-cache, must-revalidate" ); header("Pragma: no-cache" ); header("Content-Type: text/xml; charset=utf-8"); require('database.php'); $xml = '<?xml version="1.0" ?><root>'; if(isset($_GET['action']) && $_GET['action'] == 'getusers') { $last2min = date('Y-m-d h:m:s', strtotime(date('Y-m-d h:m:s'), "-2 min")); $sql = "SELECT message_id, user_name FROM fcms_chat_msgs WHERE chat_id = " . db_input($_GET['chat']) . " AND post_time <= '$last2min'"; $users_query = db_query($sql); while($users_array = db_fetch_array($users_query)) { $xml .= '<online id="' . $users_array['message_id'] . '">'; $xml .= '<user>' . htmlspecialchars($users_array['user_name']) . '</user>'; $xml .= '</online>'; } } else { $last = (isset($_GET['last']) && $_GET['last'] != '') ? $_GET['last'] : 0; $sql = "SELECT message_id, user_name, message, date_format(post_time, '%r') as post_time" . " FROM fcms_chat_msgs WHERE chat_id = " . db_input($_GET['chat']) . " AND message_id > " . $last; $message_query = db_query($sql); while($message_array = db_fetch_array($message_query)) { $xml .= '<message id="' . $message_array['message_id'] . '">'; $xml .= '<user>' . htmlspecialchars($message_array['user_name']) . '</user>'; $xml .= '<text>' . htmlspecialchars($message_array['message']) . '</text>'; $xml .= '<time>' . $message_array['post_time'] . '</time>'; $xml .= '</message>'; } } $xml .= '</root>'; echo $xml; ?> Link to comment https://forums.phpfreaks.com/topic/47744-ajax-users-online-count-in-chat/ Share on other sites More sharing options...
colandy Posted April 25, 2007 Share Posted April 25, 2007 I see no getChat text function. However, I had a similar issue with my chat app, the only way I found was to set the timeout after getting both the chat messages and the users online. Here's the code, still a work in progress but you should get the idea: function newGet() { // Get Chat Message Data var recObj= new ajaxObject('getchatdata.php', displayChatData); recObj.update(); // Get List of Users Currently using Chat var usrObj = new ajaxObject('getuserlist.php', displayUserData); usrObj.update(); //Re-Check every 2 seconds setTimeout('newGet()',2*1000); } function newSend() { var sndObj= new ajaxObject('sendchatdata.php', displayChatData); var userid='<?php echo $_SESSION["user_id"]?>'; var msg=document.getElementById('message').value; if(msg.length>100){msg=msg.substring(0,100)}; var message=('user='+userid+'&message='+msg); document.getElementById('message').value = ""; sndObj.update(message,'POST'); } function displayChatData(responseText, responseStatus){ if(responseStatus==200) { var mdiv=document.getElementById('chatwindow'); if(!mdiv){return}; var messages=responseText.split('|'); // display messages } } function displayUserData(responseText, responseStatus){ if(responseStatus==200) { var mdiv=document.getElementById('userContainer'); if(!mdiv){return}; mdiv.innerHTML=''; var messages=responseText.split('|'); // display messages } } function intitializeChat() { if(blah blah blah) { newGet(); } } The getUserList.php file woulld have to check which users have posted in the last 2 mins. But y not display all users within the chat. Easily done, as every time you check for message and users you can write to a db file using a timestamp, all records that are older than 5 seconds are for users that have left, as we update every 2 seconds. Link to comment https://forums.phpfreaks.com/topic/47744-ajax-users-online-count-in-chat/#findComment-238485 Share on other sites More sharing options...
roders25 Posted May 8, 2007 Share Posted May 8, 2007 I manage to make it work don't know if would be of any use. // JavaScript Document var sendReq = getXmlHttpRequestObject(); var receiveReq = getXmlHttpRequestObject(); var lastMessage = 0; var mTimer; //Gets the browser specific XmlHttpRequest Object function getXmlHttpRequestObject() { if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); } else { document.getElementById('u_status').innerHTML = 'Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.'; } } //Gets the current messages from the server function getUserOnline() { if (receiveReq.readyState == 4 || receiveReq.readyState == 0) { receiveReq.open("GET", 'tardis-chat/onlineuser.php?', true); receiveReq.onreadystatechange = handleUserOnline; receiveReq.send(null); } } //Function for handling the return of chat text function handleUserOnline() { if (receiveReq.readyState == 4) { var chat_div = document.getElementById('div_users'); var xmldoc = receiveReq.responseXML; var message_nodes = xmldoc.getElementsByTagName("online"); //var n_messages = message_nodes.length var user_node = message_nodes[0].getElementsByTagName("onlineuser"); chat_div.innerHTML = user_node[0].firstChild.nodeValue + ' '; chat_div.scrollTop = chat_div.scrollHeight; //lastMessage = (message_nodes[i].getAttribute('id')); mTimer = setTimeout('getUserOnline();',2000); //Refresh our chat in 2 seconds } } //This functions handles when the user presses enter. Instead of submitting the form, we //send a new message to the server and return false. function blockSubmit() { sendChatText(); return false; } Link to comment https://forums.phpfreaks.com/topic/47744-ajax-users-online-count-in-chat/#findComment-247999 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.