繁体   English   中英

在我的网站上进行聊天功能

[英]Making a chat function on my website

我想在我的网站上聊天。 很基本,我希望人们登录聊天。 当他们这样做时,我会向他们展示最后5个消息。

当该人在写东西时,会将其放入数据库中,然后使用数据库中的新文本重新加载网站。 因此它仅在用户写东西时才起作用,因为它只有在按“写”时才会更新。

为了使它更好,我正在考虑制作一个JavaScript,每3-5秒查询一次数据库的内容。

这是正确的方法还是有更好的方法?

网站上的许多聊天服务都使用Java或Flash而不是JavaScript,原因是这些语言提供了套接字支持,这意味着它们可以与服务器建立永久的开放连接以进行更新。

使用javascript,您必须使用ajax或Comet定期轮询服务器,这是一项长时间轮询的技术,但是它确实必须不时地重新建立连接。

当html5更加普及时,您将能够使用网络套接字来监听服务器的更新,但是目前,ajax或基于Flash的插件(甚至只是提供供js使用的套接字)是最可行的选择。

这样的事情将提供一个socket-swf-js类型的网桥来与您的服务器对话

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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