0

I have a question regarding the style of my .js script that updates a chatbox on my website.

Currently, the I am polling a .html file which keeps all the chat lines however I would like to change this to only poll the .html file on a new entry.

Currently working via polling every 1500 and updates the chat for all users:

// jQuery Document
$(document).ready(function(){
    //If user submits the form
    $("#submitmsg").click(function(){
        var clientmsg = $("textarea#usermsg").val();
        $.post("post.php", {text: clientmsg});              
        $("#usermsg").attr("value", "");
        clientmsg = "";
        $("textarea#usermsg").val('');
        return false;
    });

    $("textarea#usermsg").keydown(function (e) {
      if (e.keyCode == 13) {
        var clientmsg = $("textarea#usermsg").val();
        $.post("post.php", {text: clientmsg});              
        $("#usermsg").attr("value", "");
        clientmsg = "";
        $("textarea#usermsg").val('');
        return false;
      }
    });
});

setInterval (function(){ loadLog() }, 1500);

//Load the file containing the chat log
function loadLog(){     
    var oldscrollHeight = $("#chatbox").prop("scrollHeight") - 20; //Scroll height before the request
    $.ajax({
        url: "log.html",
        cache: false,
        success: function(html){        
            $("#chatbox").html(html); //Insert chat log into the #chatbox div   

            //Auto-scroll           
            var newscrollHeight = $("#chatbox").prop("scrollHeight") - 20; //Scroll height after the request
            if(newscrollHeight > oldscrollHeight){
                $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
            }               
        },
    });
}

I tried changing it to call the loadLog() function after a user presses send/enter however I noticed that this would only load the log for the user pressing send/enter and not for other users. and would not show the most recently line (perhaps it is executing the loadLog() faster than the posting to post.php and writing the log file with the new line?

//Load the file containing the chat log
function loadLog(){     
    var oldscrollHeight = $("#chatbox").prop("scrollHeight") - 20; //Scroll height before the request
    $.ajax({
        url: "log.html",
        cache: false,
        success: function(html){        
            $("#chatbox").html(html); //Insert chat log into the #chatbox div   

            //Auto-scroll           
            var newscrollHeight = $("#chatbox").prop("scrollHeight") - 20; //Scroll height after the request
            if(newscrollHeight > oldscrollHeight){
                $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
            }               
        },
    });
}

// jQuery Document
$(document).ready(function(){
    //If user submits the form
    $("#submitmsg").click(function(){
        var clientmsg = $("textarea#usermsg").val();
        $.post("post.php", {text: clientmsg});              
        $("#usermsg").attr("value", "");
        clientmsg = "";
        $("textarea#usermsg").val('');
        loadLog();
        return false;
    });

    $("textarea#usermsg").keydown(function (e) {
      if (e.keyCode == 13) {
        var clientmsg = $("textarea#usermsg").val();
        $.post("post.php", {text: clientmsg});              
        $("#usermsg").attr("value", "");
        clientmsg = "";
        $("textarea#usermsg").val('');
        loadLog();
        return false;
      }
    });
});

Is there a way to make the second version work but to update for all users not just the user sending the data?

html for chat:

<div id="chatbox"><?php
    if(file_exists("log.html") && filesize("log.html") > 0){
        $handle = fopen("log.html", "r");
        $contents = fread($handle, filesize("log.html"));
        fclose($handle);

        echo $contents;
    }
    ?>
</div>
<form name="message" action="">
    <?php if($loggedIn) { 
        echo '
        <textarea name="usermsg" id="usermsg" rows="2"></textarea>
        <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />';
    } 
    ?>
</form>

post.php

if(isset($steamprofile['steamid'])){
    $text = $_POST['text'];

    $fp = fopen("log.html", 'a');
    fwrite($fp, "<div class='msgln'>(".date("g:i A").") <b>".$steamprofile['personaname']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
    fclose($fp);
}

Thanks in advance.

Matt
  • 1,749
  • 2
  • 12
  • 26
  • You'd need to use websockets to achieve this. See http://socket.io/get-started/chat/ – Charlotte Dunois Mar 26 '16 at 17:37
  • I use hostgator as a sharedhost, will this still work as isn't this essentially node.js and require a cmd to run? Plus will I be able to insert just the socket into a part of my .php page or will i have to recreate the whole page? – Matt Mar 26 '16 at 17:39
  • It does require Node.js. Maybe this question helps you http://stackoverflow.com/questions/11334320/using-websocket-on-apache-server – Charlotte Dunois Mar 26 '16 at 17:41
  • There are several websocket libraries that you can use in php and they are easy to search for – charlietfl Mar 26 '16 at 18:02
  • I think long-polling would be easier to implement than web-sockets with your actual code. If you still wanna go with the web-socket solution I've used [this php library ratchet](http://socketo.me/) to achieve it. However with the web-sockets and shared host combination the problem isn't accessing cmd or using node since you can get SSH access from cpanel, the actual problem I think would be running a server on certain port, I believe most shared hosts block this kind of interaction. – Mark E Mar 26 '16 at 18:55
  • @MarkE I spoke to my hosting and they said they can open specific ports. Can I use any port or does it have to be a specific one? – Matt Mar 26 '16 at 20:33
  • @Matt whichever port you like, you set it at the beginning of you application. – Mark E Mar 26 '16 at 21:36

0 Answers0