简体   繁体   中英

Making a chat function on my website

I want tto make a chat on my site. Very basic I want people to login to chat. And when they do that I show them the last 5 messeges.

When the person is writting something it is put into the database, and then reloads the site, with the new text from the database. So it only works when the user writes something, because it will only update when he press 'Write'.

To make it even better I am thinking of making a javascript to look up the content of the database and every 3-5 seconds.

Is that the right way to do it or is there a better way??

a lot of chat services on websites use java or flash rather than javascript, the reason is that those languages provide socket support which means they can have a permanent open connection to the server for updates.

with javascript you have to poll the server at a regular intervals using ajax or comet which is a technique for long polling, but it does have to re-establish connections every now and then.

when html5 is more widespread you will be able to use web-sockets to listen to the server for updates, but for now ajax or a flash based plugin (even to just provide sockets for js to use) is the most viable option.

something like this will provide a socket-swf-js type bridge to talk to your server

http://code.google.com/p/jssockets/

Yes, recently i've made a simple groupchat application with javascript and php and i used to check the text file where all the chat messages i'm writing to for every 2 secs....             

<div id="chatbox"></div>//html div element where i've to paste the message data



$("#submitmsg").click(function(){
   $.post("post.php", {text: send_mymsg});//where am sending my data to a php file to write into a html file "log.html"
}

function loadLog(){ 
    $.ajax({
            url: "log.html",
            cache: false,
            success: function(html){
                $("#chatbox").html(html);
         });
}
setInterval (loadLog,2000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM