slyte33 Posted October 22, 2009 Share Posted October 22, 2009 Hello everyone, I created a text-based RPG game, which gives players the opportunity to use the chat function. What I am currently using is a chat window to the left of my game, with AJAX. I have an AJAX code that looks like this: <script type="text/javascript"> $(document).ready(function(){ $('form').submit(function(){ element = $('input[@name=input]').get(0); mymessage = element.value; myname = $('input[@name=name]').get(0).value; $.post('chatmsgs.php', {message: mymessage, name: myname}); element.value = ''; return false; }); updateChat(); }); function updateChat() { num = Math.random(); $('div.chatwindow').load('chatmsgs.php?'+num); setTimeout('updateChat()', 1000); } </script> That simply updates the chat window every 1 second, thus lagging a lot of people. I was wondering how I could make this only load the chat window when someone posts a message, if needed, here is my chat scripts: chatmsgs.php <?php include("lib.php"); include("chatfunctions.php"); $player = check_user($secret_key, $db); $muted = $db->execute("select * from mute where player_id=? and time>=?", array($player->id, time())); $ismute = $muted->fetchrow(); $ti = time(); $currenttime = time(); $query = $db->execute("delete from `mute` where `time` < ?", array($currenttime)); if ($player->chattime < time()) { $query = $db->execute("update `players` set `chattime`=? where `id`=? and `chattime` <= ?", array($player->chattime = 0, $player->id, $ti)); } if ($query3->recordcount() >= 1) { echo "<font color=red>You have been muted from the chatroom for<br><br> "; $timeleft2 = $yes['time']-time(); if($timeleft2 >= 86400) { echo ""; echo floor($timeleft2/86400)," days "; } else { echo ""; } if($timeleft2 >= 3600) { echo ""; echo floor($timeleft2/3600)-24*floor($timeleft2/86400)," hours "; } else { echo ""; } if($timeleft2 >= 60) { echo ""; echo floor($timeleft2/60)-60*floor($timeleft2/3600)," minutes "; } else { echo ""; } if($timeleft2 > 0) { echo ""; echo floor($timeleft2)-60*floor($timeleft2/60)," seconds"; } echo "<br><br>Reason: " . $yes['reason'] . ""; exit; } ?> <? $query = $db->execute("select `id` from `chat`"); { $chatt = $query->fetchrow(); } ?> <style> a.admin { color: #aa0000;} a.secretary { color: #006633; } a.moderator { color: #0085A3;} a.helper { color: #660099; } </style> <? $time = time() + 10; $t = time() + 3600; $chat = $db->execute("select * from `players` where `chattime`=? and `id`=?", array($player->chattime > time(), $player->id)); if ($player->chattime >= $time) { $insert['muteid'] = 0; $insert['player_id'] = $player->id; $insert['time'] = $t; $query = $db->autoexecute('mute', $insert, 'INSERT'); } if(isset($_POST['message'])) { if ($player->chattime <= 0) { $chattime = time() + 3; } else { $chattime = 3; } $query = $db->execute("update `players` set `posts`=?, `chattime`=? where `id`=?", array($player->posts + 1, $player->chattime + $chattime, $player->id)); if(!empty($_POST['message'])) { $insert['chatline'] = strip_tags($_POST['message']); $message = strip_tags($message); $message = LangFilter($message, ENT_QUOTES); $insert['user'] = $player->username; $insert['guildid'] = $guild['id']; $insert['time'] = time(); $query = $db->autoexecute('chat', $insert, 'INSERT'); } } else { $query = $db->execute("select * from `chat` order by `id` desc limit 10"); } while ($msgs = $query->fetchrow()) { $datestr = $msgs['time']; $time = date('m/d H:i', $datestr); $user = $msgs['user'] . ""; $rank = $db->GetOne("select `rank` from `players` where `username`=?", array($user)); $id2 = $db->GetOne("select `id` from `players` where `username`=?", array($user)); $messagestr = "" . $msgs['chatline'] . ""; $messagestr1 = StripSlashes($messagestr); $messagestr2 = LangFilter($messagestr1); $messagestr3 = Convert($messagestr2); $messagestr4 = bbCode($messagestr3); echo " <font color=white> $messagestr4 <br/>------------------------------</font><br>"; } else { echo "<a href=profile.php?id=" . $id2 . ">" . $user . "</a>: "; echo " <font color=white> $messagestr4 <br/>------------------------------<br></font>"; } } } ?> chat.php(include <div class=chatwindow></div> - to load chatmsgs.php with above ajax script) <?php include("lib.php"); $player = check_user($secret_key, $db); $title = "Chat"; include("templates/private_header.php"); ?> <? if ($_GET['act'] == "delete") { $id=$_GET['id']; $query = $db->execute("delete from `chat` where `id`=?", array($id)); } ?> <table class="back" style="margin:0; padding:0; width:98%;"> <tr> <td class="back" style="padding:0 0 0 2px;width:100%;"> <div style="margin:6px 0px 6px 0px;"> <div class="rc_box3"> <div class="rc_top3"><div> </div></div> <div class="rc_content3"> <div style="padding: 0px 0px 0px 0px;"> <div style="float:left;vertical-align:top;text-align:left;"> <form> <input type="text" name="input" value="" size="50" /> <input type="submit" value="post" /> </form> <br> <div class="chatwindow"> <p> </div> </div> <div style="clear:both;"> </div> </div> </div> <div class="rc_bottom3"><div> </div></div> </td> </tr> </table> </center> <? include("templates/private_header2.php"); ?> So basically Im just curious is to whether I can keep a session of the most recent chat id, and if a new one is posted, update chatmsgs.php Link to comment https://forums.phpfreaks.com/topic/178623-keeping-a-_session-variable-of-the-last-chat-id/ Share on other sites More sharing options...
nadeemshafi9 Posted October 22, 2009 Share Posted October 22, 2009 heres a quick and naive one as i havent faced this issue before but i have given it thoght before, im sure somone will give you a much better theory, but here it goes. put a random number into a table feild called db change tracker, instead of ajaxing your whole chat resulty thingy, ajax that random feild number record and then keep it, then just keep ajaxing to check if it changes, when ever there is an update to the databse throgh your chat, change the random number and then when you ajax to get the number your js will know its different from the one it just had, then ajax tro get the chat. Link to comment https://forums.phpfreaks.com/topic/178623-keeping-a-_session-variable-of-the-last-chat-id/#findComment-942150 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.