2

我开发了一个基于 Web 的应用程序,其中登录用户应该每 3 秒向服务器发送一条消息,告诉他他仍然在线。然后由服务器处理该消息,并在 Mysql 中调用一个存储过程将用户的状态设置为在线。我已经研究过比较 Comet 和 Ajax 的类似问题(此处此处),但考虑到 3 秒延迟是可以接受的,并且系统中最多有 1000 个用户在线,使用 Ajax 是明智的选择还是应该使用 Comet ?

4

2 回答 2

1

对于彗星这种特征比较合适:

  • 您的客户发送消息(我在线)
  • 您的服务器广播处理后的消息(用户 X 仍然在线)

以 ajax 方式,您只向服务器提供消息。

为了以ajax方式获得“广播效果”。你最终会做一些类似于彗星的事情,但带宽效率较低。

阿贾克斯:

  • 客户端发送服务器 - 我在
  • 服务器进程
  • 服务器发送回用户的客户端列表。

在这种情况下,每个客户端每 3 秒向数据库询问一次COMPLETE “in”列表。

在彗星中:

  • 客户端 X 发送服务器 - 我在
  • 服务器进程
  • 服务器向客户端S发回用户 X 仍然在线

在这种情况下,每个客户端每 3 秒告诉服务器他在其中。服务器向每个连接的客户端发回x 仍在

Comet 只是一种将消息广播回来并将消息推送到客户端的技术 Ajax 是一种无需刷新所有页面即可将客户端信息推送到服务器的技术。

引用维基百科: http ://en.wikipedia.org/wiki/Comet_%28programming%29

Comet 还有其他几个名字,包括Ajax PushReverse Ajax、Two-way-web、HTTP Streaming 和 HTTP server push 等。

所以去彗星吧:)

如果你不广播任何东西,那么简单的 Ajax 是最好的选择

于 2012-05-21T08:19:58.447 回答
0

In this particular case, since you do not need to send any information from the server to the client(s), I believe Ajax is the more appropriate solution. Every three seconds, the client tells the server it is connected, the database is updated, and you're done.

It could certainly be done using Comet, in which case you would basically ping each registered client to see if it is still connected. But, you would still need to run a query on the database for each client that responds, plus you would still need the client to notify the server on its initial connection. So, it seems to me that Comet would be more trouble than it's worth. The only thing that might make sense is if you could ping each registered client and store the responses in memory, then once all clients have been pinged you can run one single query to update all of their statuses. This would give you the added bonus of knowing as soon as a client disconnects as opposed to waiting for a timeout. Unfortunately, that is beyond the scope of my expertise with Comet so, at this point, I can't help to actually implement it.

于 2012-06-24T15:36:59.050 回答