热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

走出这个世界的Comet编程和基于网络的聊天-outofthisworldCometprogrammingandaweb-basedchat

CurrentProjectSetupIvebeenworkingonaweb-basedchat,similartoFacebookchat.Atthecurre

Current Project Setup

I've been working on a web-based chat, similar to Facebook chat. At the current state, I listen for incoming chats and check for new messages in an existing chat is by doing...

我一直在进行基于网络的聊天,类似于Facebook聊天。在当前状态下,我会监听传入聊天并检查现有聊天中的新消息是做...

setTimeout(function() { listenForIncomingChat() }, 500);
setTimeout(function() { checkForIncomingMessages( ...params... ) }, 500);

... so doing the setTimeout() makes sure these functions are always running. Depending on how many chat windows I have open, Firebug's console can go crazy with POSTs to the server :)

...所以做setTimeout()可以确保这些函数始终在运行。根据我打开的聊天窗口数量,Firebug的控制台可能会疯狂地将POST发送到服务器:)

Obviously this is really inefficient, but it is the only way I could get things to work. Now I'm looking for the ways to make it better, to do it correctly!

显然这是非常低效的,但这是我能让事情发挥作用的唯一方法。现在我正在寻找让它变得更好的方法,正确地做到这一点!

Some Research

Now, I have heard about Comet Programming and that this is the way to open a long-lived HTTP connection with the server, but I'm not familiar with the technology or the ideas behind Comet. WebSockets for HTML5 is probably even better, but since that is not in full swing nor is it supported by all browsers, I'll stick with what works.

现在,我听说过Comet Programming,这是与服务器打开长期HTTP连接的方法,但我不熟悉Comet背后的技术或思想。用于HTML5的WebSockets甚至可能更好,但由于这并不是全面展开,也不是所有浏览器都支持,我会坚持使用有效的方法。

According to Wikipedia, there are several ways to develop with the Comet style: Streaming (hidden iFrame, XMLHttpRequest) or AJAX with long polling (XMLHttpRequest, Script tag). However, I don't know anything about this. I've also read about the AJAX Push Engine (APE) and it looks cool, but I don't want to use a third-party for the time being.

根据维基百科,有几种使用Comet风格开发的方法:Streaming(隐藏的iFrame,XMLHttpRequest)或带有长轮询的AJAX(XMLHttpRequest,Script标签)。但是,我对此一无所知。我也读过关于AJAX推送引擎(APE)的内容并且看起来很酷,但我暂时不想使用第三方。

I've recently stumbled upon WebChat 2.0 so I'm going to be looking through the source code to try and understand how it all works.

我最近偶然发现了WebChat 2.0,所以我将通过源代码来查看它是如何工作的。

On to the question(s)

So where can I find example code/tutorials on how to get started with this kind of project? How would I implement the Comet technique? How do I set up the long-lived HTTP connection with the server?

那么我在哪里可以找到有关如何开始使用此类项目的示例代码/教程?我如何实施Comet技术?如何设置与服务器的长期HTTP连接?

1 个解决方案

#1


9  

Here's an example of a chatroom using node.js, source code here.

这是一个使用node.js的聊天室示例,这里是源代码。

I believe the client uses polling, but this example is interesting because the server side is in JS too, and node.js is efficient for this type of stuff.

我相信客户端使用轮询,但这个例子很有趣,因为服务器端也在JS中,而node.js对于这种类型的东西是有效的。


推荐阅读
author-avatar
遗忘不能忘
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有