是否可以异步处理

发布于 2024-08-31 08:36:43 字数 467 浏览 4 评论 0原文

我有一个要求,我需要向我的客户发送持续更新。在这种情况下,客户端是浏览器。我们有一些每秒更新的数据,因此一旦客户端连接到我们的服务器,我们就会保持持久连接并不断将数据推送到客户端。

我正在服务器端寻找此实现的建议。基本上我需要的是这样的: 1.客户端连接到服务器。我维护套接字和有关套接字的元数据。元数据包含需要发送给该客户端的更新内容 2. 服务器进程现在等待新的客户端连接 3. 另一个进程将拥有所有打开的套接字的列表,并将遍历每个套接字并在需要时发送更新。

我们可以在 Apache 模块中做这样的事情吗: 1. Apache进程获取新连接。它维护连接的状态。它将状态保存在某个全局内存中并返回到根进程以表示它已完成,以便它可以接受新连接 2. Apache 进程虽然已将状态返回给根进程,但它也在并行执行,遍历其全局存储并向客户端发送更新(如果有)。

那么 Apache 进程可以做这些事情: 1. 拥有多个与之关联的连接 2、异步等待新连接,同时处理之前的连接?

I have a requirement where I need to send continuous updates to my clients. Client is browser in this case. We have some data which updates every sec, so once client connects to our server, we maintain a persistent connection and keep pushing data to the client.

I am looking for suggestions of this implementation at the server end. Basically what I need is this:
1. client connects to server. I maintain the socket and metadata about the socket. metadata contains what updates need to be send to this client
2. server process now waits for new client connections
3. One other process will have the list of all the sockets opened and will go through each of them and send the updates if required.

Can we do something like this in Apache module:
1. Apache process gets the new connection. It maintains the state for the connection. It keeps the state in some global memory and returns back to root process to signify that it is done so that it can accept the new connection
2. the Apache process though has returned the status to root process but it is also executing in parallel where it going through its global store and sending updates to the client, if any.

So can a Apache process do these things:
1. Have more than one connection associated with it
2. Asynchronously waiting for new connection and at the same time processing the previous connections?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-09-07 08:36:43

这是一种复杂且低效的更新模型。您的服务器将尝试更新已关闭的客户端。服务器必须维护所有客户端数据和元数据(上次更新时间等)。

通常,对于连续更新,ajax 用于轮询模型。客户端有一个 JavaScript 计时器,当它触发时,会触发提供更新数据的服务。客户端继续定期获取更新,而无需编写 apache 模块。

这个模型适用于您的场景吗?

选择轮询而不是推送的更多理由
Periodic_Refresh

This is a complicated and ineffecient model of updating. Your server will try to update clients that have closed down. And the server has to maintain all that client data and meta data (last update time, etc).

Usually, for continuous updates ajax is used in a polling model. The client has a javascript timer that when it fires, hits a service that provides updated data. The client continues to get updates at regular intervals without having to write an apache module.

Would this model work for your scenario?

More reasons to opt for poll instead of push
Periodic_Refresh

晨曦÷微暖 2024-09-07 08:36:43

用一个小补丁来恢复暂停的 mpm_event 连接,我有一个异步 Apache 模块正在工作。有了这个,您可以进行改进的轮询:

  1. javascript 连接到 Apache 并请求更新;
  2. 如果没有更新,则模块不会立即应答,而是使用 SUSPENDED;
  3. 一段时间后,在更新或超时发生后,回调会在某处触发;
  4. 回调向客户端提供更新(或“无更新”消息)并恢复连接;
  5. 客户端转到步骤 1,重复轮询,使用 Keep-Alive 将使用相同的连接。

这样可以减少客户端和服务器之间的往返次数,并且客户端可以立即收到更新。 (这被称为 Comet 的反向 Ajax,据我所知)。

With a little patch to resume a SUSPENDED mpm_event connection, I've got an asynchronous Apache module working. With this you can do the improved polling:

  1. javascript connects to Apache and asks for an update;
  2. if there's no updates, then instead of answering immediately the module uses SUSPENDED;
  3. some time later, after an update or a timeout happens, callback fires somewhere;
  4. callback gives an update (or a "no updates" message) to the client and resumes the connection;
  5. client goes to step 1, repeating the poll which with Keep-Alive will use the same connection.

That way the number of roundtrips between the client and the server can be decreased and the client receives the update immediately. (This is known as Comet's Reverse Ajax, AFAIK).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文