【问题标题】:How IMAP idle works?IMAP 空闲如何工作?
【发布时间】:2011-01-06 04:22:21
【问题描述】:

有人可以向我解释一下 IMAP IDLE 的工作原理吗?它是否为它打开的每个连接创建一个新进程?我可以以某种方式使用 eventmachine 吗?

我正在尝试用后台工作人员在 heroku 上的 ruby​​ 中实现它。有什么想法吗?

【问题讨论】:

  • 您想知道什么是 IMAP IDLE 以及何时需要将哪些数据发送回客户端,或者您想知道如何在现有的 IMAP 服务器中实现 IMAP IDLE?
  • IMAP IDLE 是 IMAP 协议的一部分。所以是的,dkarp 的问题是:你想知道协议规范是什么,或者如何在网站上实现它(我猜你没有制作 IMAP 服务器,因为你在谈论 heroku)。
  • 查看这里以获得答案:stackoverflow.com/a/1818718/459863

标签: ruby heroku imap eventmachine


【解决方案1】:

在 Ruby 2.0 及更高版本中,有一个空闲方法,它接受每次收到未标记响应时都会调用的代码块。收到此响应后,您需要中断并拉取进来的电子邮件。空闲调用也是阻塞的,因此如果要使其保持异步,则需要在线程中执行此操作。

这是一个示例(在这种情况下,@mailbox 是 Net::IMAP 的一个实例):

def start_listener()
    @idler_thread = Thread.new do
        # Run this forever. You can kill the thread when you're done. IMAP lib will send the 
        # DONE for you if it detects this thread terminating
        loop do
            begin
                @mailbox.select("INBOX")
                @mailbox.idle do |resp|
                    # You'll get all the things from the server. For new emails you're only 
                    # interested in EXISTS ones
                    if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
                        # Got something. Send DONE. This breaks you out of the blocking call
                        @mailbox.idle_done
                    end
                end
                # We're out, which means there are some emails ready for us.
                # Go do a seach for UNSEEN and fetch them.
                process_emails()
            rescue Net::IMAP::Error => imap_err
                # Socket probably timed out
            rescue Exception => gen_err
                puts "Something went terribly wrong: #{e.messsage}"
            end
        end
    end
end

【讨论】:

【解决方案2】:

IMAP IDLE 是邮件服务器实现可以支持的一项功能,它允许实时通知。 [Wikipedia]

IDLE 命令可用于任何将“IDLE”作为 CAPABILITY 命令支持的功能之一返回的 IMAP4 服务器实现。

当客户端准备好接受未经请求的邮箱更新消息时,从客户端向服务器发送 IDLE 命令。服务器使用继续(“+”)响应请求对 IDLE 命令的响应。 IDLE 命令保持活动状态,直到客户端响应继续,只要 IDLE 命令处于活动状态,服务器现在就可以随时发送未标记的 EXISTS、EXPUNGE 和其他消息。

IDLE 命令因收到来自客户端的“DONE”继续而终止;这样的响应满足服务器的继续请求。 [...] 客户端不得在服务器等待 DONE 时发送命令,因为服务器将无法区分命令和延续。

[RFC 2177 - IMAP4 IDLE command]

【讨论】:

    猜你喜欢
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    相关资源
    最近更新 更多