是否可以异步处理
我有一个要求,我需要向我的客户发送持续更新。在这种情况下,客户端是浏览器。我们有一些每秒更新的数据,因此一旦客户端连接到我们的服务器,我们就会保持持久连接并不断将数据推送到客户端。
我正在服务器端寻找此实现的建议。基本上我需要的是这样的: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种复杂且低效的更新模型。您的服务器将尝试更新已关闭的客户端。服务器必须维护所有客户端数据和元数据(上次更新时间等)。
通常,对于连续更新,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
用一个小补丁来恢复暂停的 mpm_event 连接,我有一个异步 Apache 模块正在工作。有了这个,您可以进行改进的轮询:
这样可以减少客户端和服务器之间的往返次数,并且客户端可以立即收到更新。 (这被称为 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:
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).