How IMAP idle works?

后端 未结 2 913
挽巷
挽巷 2020-12-30 16:42

Can someone explain to me how IMAP IDLE works? Does it fork a new process for each connection that it opens? Can I somehow use eventmachine with it?

I am trying to

2条回答
  •  囚心锁ツ
    2020-12-30 17:11

    In Ruby 2.0 and up, there's an idle method that accepts a code block that will be called every time you get an untagged response. Once you got this response, you need to break out and pull the emails that came in. The idle call is also blocking, so you need to do this in a thread if you want to keep it asynchronous.

    Here's a sample (@mailbox is an instance of Net::IMAP in this case):

    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
    

提交回复
热议问题