简体   繁体   English

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

[英]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. 当他们这样做时,我会向他们展示最后5个消息。

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

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

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

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

something like this will provide a socket-swf-js type bridge to talk to your server 这样的事情将提供一个socket-swf-js类型的网桥来与您的服务器对话

http://code.google.com/p/jssockets/ 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