欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ruby實(shí)現(xiàn)郵件主動(dòng)推送觸發(fā)程序

 更新時(shí)間:2015年01月04日 09:52:28   投稿:junjie  
這篇文章主要介紹了Ruby實(shí)現(xiàn)郵件主動(dòng)推送觸發(fā)程序,本文給出了客戶端輪詢和服務(wù)器主動(dòng)推送的代碼實(shí)例,需要的朋友可以參考下

郵件服務(wù)器接收到郵件后,service push通知程序。有什么辦法實(shí)現(xiàn)嗎?

1、客戶端輪詢
2、服務(wù)器主動(dòng)推送。

首先熟悉一下,收發(fā)郵件的協(xié)議:
Net::SMTP(發(fā)送郵件)
Net::POP3(接收郵件)
Net::IMAP(接收郵件)

網(wǎng)上很多用pop3收郵件的例子,但是用pop3收郵件只能獲取收件箱里面所有郵件,郵件是否已讀等標(biāo)記無(wú)法獲取,使用imap協(xié)議則避免了這個(gè)尷尬,imap不僅能獲得一個(gè)郵件的詳細(xì)信息(比如是否已讀,是否回復(fù)),它還允許用戶更改郵件的標(biāo)記,但是目前支持imap協(xié)議的郵件服務(wù)器并不多,我知道的只有21cn和gmail,下面的例子中使用了代理 、SSL認(rèn)證多個(gè)內(nèi)容,請(qǐng)大家參考。

imap郵件,都是按需索取,也就是說(shuō),當(dāng)你得到一個(gè)Message的對(duì)象時(shí),其實(shí)里面什么信息都沒(méi)有,當(dāng)你在這個(gè)對(duì)象里用get方法取得信息時(shí),比如getSubject,那么Message對(duì)象會(huì)重新訪問(wèn)郵件服務(wù)器來(lái)得到這個(gè)消息的 ,所以在得到所有所需信息之前,不可以關(guān)閉目錄,更不可以斷開(kāi)連接。
如果實(shí)在想在關(guān)閉目錄或者連接后操作Message對(duì)象的話,需要使用Folder對(duì)象的fetch方法得到所需信息。

一:客戶端輪詢

下邊用pop3和imap顯示一下輪詢?cè)L問(wèn)獲取郵件的例子:

POP3輪詢:

復(fù)制代碼 代碼如下:

loop do
require 'net/pop'
pop = Net::POP3.new('EMAILSERVICE')
pop.start('USENAME', 'PASSWORD')           
if pop.mails.empty?
  puts 'No mail.'
else
  pop.each_mail do |m|
    m.pop do |chunk|  
      p chunk
    end
  end
  puts "#{pop.mails.size} mails popped."
end
pop.finish
sleep(10)
end

imap輪詢:

復(fù)制代碼 代碼如下:

loop do
require 'net/imap'
imap = Net::IMAP.new('EMAILSERVICE')
imap.login "USERNAME", "PASSWORD"
imap.examine('INBOX')
imap.search(["BEFORE", "29-Oct-2014", "SINCE", "28-Oct-2014"]).each do |message_id|
   envelope = imap.fetch(message_id, "ENVELOPE")[0].attr["ENVELOPE"]
   puts "#{envelope.from[0].name}: \t#{envelope.subject}"
end
sleep(10)
end

二:服務(wù)器主動(dòng)推送

下邊實(shí)現(xiàn)一種服務(wù)器主動(dòng)推送方式:(IMAP.IDLE)

這是一種介于pull和Persistent TCP/IP之間的技術(shù):long polling(長(zhǎng)輪詢)。原理是客戶端每次對(duì)服務(wù)的請(qǐng)求都被服務(wù)端hold住,等到有message返回或time out之后,會(huì)再次主動(dòng)發(fā)起請(qǐng)求,等待message的到達(dá)。這種模式不需要保持心跳,也不需要持續(xù)TCP的占用,比較適合頁(yè)面端及時(shí)消息的推送。

復(fù)制代碼 代碼如下:

SERVER = 'EMAILSERVICE'
USERNAME = 'USERNAME'
PW = 'PASSWORD'
require 'net/imap'

# Extend support for idle command. See online.
# http://www.ruby-forum.com/topic/50828
# https://gist.github.com/jem/2783772
# but that was wrong. see /opt/ruby-1.9.1-p243/lib/net/imap.rb.
class Net::IMAP
  def idle
    cmd = "IDLE"
    synchronize do
      @idle_tag = generate_tag
      put_string(@idle_tag + " " + cmd)
      put_string(CRLF)
    end
  end

  def say_done
    cmd = "DONE"
    synchronize do
      put_string(cmd)
      put_string(CRLF)
    end
  end

  def await_done_confirmation
    synchronize do
      get_tagged_response(@idle_tag, nil)
      puts 'just got confirmation'
    end
  end
end

class Remailer
  attr_reader :imap

  public
  def initialize
    @imap = nil
    @mailer = nil
    start_imap
  end

  def tidy
    stop_imap
  end

  def print_pust
       envelope = @imap.fetch(-1, "ENVELOPE")[0].attr["ENVELOPE"]
       puts "From:#{envelope.from[0].name}\t Subject: #{envelope.subject}"
  end

  def bounce_idle
    # Bounces the idle command.
    @imap.say_done
    @imap.await_done_confirmation
    # Do a manual check, just in case things aren't working properly.
    @imap.idle
  end

  private
  def start_imap
    @imap = Net::IMAP.new('pop.i-click.com')
    @imap.login USERNAME, PW
    @imap.select 'INBOX'

    # Add handler.
    @imap.add_response_handler do |resp|
      if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
        @imap.say_done
        Thread.new do
          @imap.await_done_confirmation
          print_pust
          @imap.idle
        end
      end
    end
    @imap.idle
  end

  def stop_imap
    @imap.done
  end

end

begin
  Net::IMAP.debug = true
  r = Remailer.new
  loop do
    puts 'bouncing...'
    r.bounce_idle
    sleep 15*60
    #一般設(shè)置15分鐘無(wú)操作保持長(zhǎng)鏈接
  end
ensure
  r.tidy
end

相關(guān)文章

最新評(píng)論