The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> A Few Questions...
Nicholas_Roge
post Aug 4 2009, 03:10 PM
Post #1





Group: Members
Posts: 2
Joined: 4-August 09
Member No.: 9,325



I am trying to create a chatbox, but I've run into three problems that I really need help with.

Before I begin, here's the code I'm currently using (note that some of it is nonfunctional (namely the users column)). It should be noted that I'm using some of the MyBB functions and that one of my questions deals with server side code:

chatBox.php:
CODE

<?php
define('THIS_SCRIPT', 'chatBox.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Digital Resarch Project - {ChatBox}</title>
</head>

<script type="text/javascript">
var xmlhttp;
var timeout;

if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
    alert("Your browser does not support XMLHTTP!");
}
  
  
function loadLogin(){
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState==4)
        {
            document.getElementById('loginBox').innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "chatBoxFunctions.php?action=recieve",true);
    xmlhttp.send(null);
}
function recieveData()
{
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4)
        {
            document.getElementById('chatText').innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "chatBoxFunctions.php?action=recieve", true);
    xmlhttp.send(null);
    
    timeout = setTimeout("recieveData()", 2000);
}
function sendData()
{
    if(document.chatbox.message.value!=""){
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState==4)
            {
                document.chatbox.message.value="";
            }
        }
        xmlhttp.open("GET","chatBoxFunctions.php?action=sending&message="+document.chatbox.message.value,true);
        xmlhttp.send(null);
    }else{
        alert('The message box cannot be left blank');
    }
}
</script>

<style type="text/css">
    body{
        margin:0;
    }
    #chatHolder.td,#chatHolder.tr{
        margin:0;
    }
    #chatText{
        height:400px;
        width:600px;
        overflow:scroll;
    }
</style>

<body onload="recieveData();loadLogin();">
    <form name="chatbox">
    <table cellspacing="0" cellpadding="0" width="100%" id="chatHolder">
        <tr id="chatHeader">
            <td colspan="2" width="600" align="center"><strong>The Official DRP Chat</strong></td>
            <td width="100px" align="center"><strong>Users</strong></td>
        </tr>
        <tr id="textAreas">
            <td colspan="2"><div id="chatText"></div></td>
            <td id="userTextHolder"><textarea name="userText" rows="20" disable="disabled" style="width:100%">USERS</textarea></td>
        </tr>
        <tr>
            <td id="messageArea" align="center"><textarea name="message" rows="4" style="width:90%" onkeydown="checkKey()"></textarea><br />
            <input type="button" value="Submit" onclick="sendData()"/></td>
            <td id="volume" width="1px" valign="bottom" align="center"><input name="muteCheckbox" type="checkbox" /><br />Mute</td>
            <td id="loginBox"></td>
        </tr>
    </table>
    </form>
</body>
</html>


chatBoxFunctions.php:
CODE
<?php
define('IN_MYBB', 1);
define('THIS_SCRIPT', 'index.php');

require_once "./global.php";

if($_GET['action']=='sending'){
    for($i=0;$i<strlen($_GET['message']);$i++){
        if(substr($_GET['message'],$i,1)=="<"){
            $userData['message'] .= "<";
        }else if(substr($_GET['message'],$i,1)==">"){
            $userData['message'] .= ">";
        }else if(substr($_GET['message'],$i,1)=="\""){
            $userData['message'] .= """;
        }else if(substr($_GET['message'],$i,1)=="'"){
            $userData['message'] .= "’";
        }else if(substr($_GET['message'],$i,1)=="\\"){
            $userData['message'] .= "⁄";
        }else if(substr($_GET['message'],$i,2)=="  "){
            $userData['message'] .= "  ";
        }else{
            $userData['message'] .= substr($_GET['message'],$i,1);
        }
    }
    
    $userData['uid']=$mybb->user['uid'];
    
    $query=$db->fetch_array($db->query("SELECT username FROM mybb_users WHERE uid=".$userData['uid']));
    $userData['username']=$query['username'];
    $userData['datePosted']= time();
    
    $db->query("INSERT INTO chatLog (message,poster,name,datePosted)
               VALUES ('".$userData['message']."',".$mybb->user['uid'].",'".$userData['username']."',".$userData['datePosted'].")"
    );
    echo "Message Submitted";
}
if($_GET['action']=='recieve'){
    $i=1;
    
    $timeNow = time();
    $query = $db->query("Select * FROM chatLog WHERE datePosted<".$timeNow." ORDER BY clid DESC Limit 0,100");
    if($query){
        $responce="";
        while($values = $db->fetch_array($query)){
            if(substr($values['message'],0,4)=="/me "){
                if($i==1){
                    $response.="<strong>".$values['name']."</strong> ".substr($values['message'],4)."<br /><br />";
                }else{
                    $response.="<strong>".$values['name']."</strong> ".substr($values['message'],4)."<br/>";
                }
            }else{
                if($i==1){
                    $response.="<strong>".$values['name']."</strong> says:  ".$values['message']."<br /><br />";
                }else{
                    $response.="<strong>".$values['name']."</strong> says:  ".$values['message']."<br/>";
                }
            }
            $i++;
        }
        echo $response;
    }
}

?>


_____________________________________
So on to the problems in order of severity:
  • The PHP question: how would I just check to see if the user is already up to date in the chat or if their is a new message to download? Because as it stands, I'm using a query every two seconds, and that's gonna put a straign on the server. This leads me into my next question:
  • I'm getting the new messages every two seconds, so there will be a two second lag between the time a user replies and when it's viewable, because if I don't, the SQL query will occasionaly not complete (at least that's what I assume is happening), and it causes the chat area to go blank. To me, that's just to much time to wait for a new reply. Any ideas to resolve this?
  • How do I make it so that the scrollbar for the x axis doesn't show up, and the text wraps?
  • When a new reply is recieved, I need to make the scroll bar for the chatHolder td to scroll to the bottom. As a side question, is there a way to return where the scroll bar is at (the x or y coordinate)?
  • How would I make a list of currently active users? I think the session method, but I'm not sure at the moment.

And before someone says it, I've already tried google.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 03:24 AM