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

时间:2011-12-09 09:17:19

标签: javascript jquery asp.net chat

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

当该人正在写东西时,将其放入数据库,然后使用数据库中的新文本重新加载该站点。所以它只在用户写东西时才有效,因为它只会在按下“写入”时更新。

为了让它更好,我正在考虑使用javascript来查找数据库的内容,每隔3-5秒。

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

2 个答案:

答案 0 :(得分:3)

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

使用javascript你必须使用ajax或comet定期轮询服务器,这是一种长轮询技术,但它必须时不时地重新建立连接。

当html5更广泛时,您将能够使用Web套接字来监听服务器的更新,但是现在ajax或基于闪存的插件(甚至只是为js提供套接字)是最可行的选择

这样的东西将提供一个socket-swf-js类型的桥接器来与服务器通信

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

答案 1 :(得分:1)

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);
相关问题