2013-03-27 78 views
1

需要通過Smpp發送和接收SMS消息,Register Number,收到systemID和密碼,但無法連接。連接到項目gem'ruby-smpp',使用示例,它僅更改了systemID和密碼的值。在Ruby on Rails中使用Smpp

在日誌

<- (BindTransceiver) len = 37 cmd = 9 status = 0 seq = 1364360797 (<systemID><password>) 
Hex dump follows: 
<- 00000000: 0000 0025 0000 0009 0000 0000 5152 7e5d | ...% ........ QR ~] 
<- 00000010: 3531 3639 3030 0068 4649 6b4b 7d7d 7a00 | <systemID>.<password>. 
<- 00000020: 0034 0001 00 | .4 ... 

Starting enquire link timer (with 10s interval) 
Delegate: transceiver unbound 

在控制檯:

Connecting to SMSC ... 
MT: Disconnected. Reconnecting in 5 seconds .. 
MT: Disconnected. Reconnecting in 5 seconds .. 
MT: Disconnected. Reconnecting in 5 seconds .. 
MT: Disconnected. Reconnecting in 5 seconds .. 
MT: Disconnected. Reconnecting in 5 seconds .. 

請告訴我,我不這樣做,也許在配置到別的東西來添加或更改?和SMPP連接,按照我的理解,它僅適用於一個特定的IP地址,但在服務器上,並在本地機器上的日誌是一樣的

class Gateway 
    include KeyboardHandler 

    # MT id counter. 
    @@mt_id = 0 

    # expose SMPP transceiver's send_mt method 
    def self.send_mt(*args) 
    @@mt_id += 1 
    @@tx.send_mt(@@mt_id, *args) 
    end 

    def logger 
    Smpp::Base.logger 
    end 

    def start(config) 
    # The transceiver sends MT messages to the SMSC. It needs a storage with Hash-like 
    # semantics to map SMSC message IDs to your own message IDs. 
    pdr_storage = {} 

    # Run EventMachine in loop so we can reconnect when the SMSC drops our connection. 
    puts "Connecting to SMSC..." 
    loop do 
     EventMachine::run do 
     @@tx = EventMachine::connect(
      config[:host], 
      config[:port], 
      Smpp::Transceiver, 
      config, 
      self # delegate that will receive callbacks on MOs and DRs and other events 
     ) 
     print "MT: " 
     $stdout.flush 

     # Start consuming MT messages (in this case, from the console) 
     # Normally, you'd hook this up to a message queue such as Starling 
     # or ActiveMQ via STOMP. 
     EventMachine::open_keyboard(KeyboardHandler) 
     end 
     puts "Disconnected. Reconnecting in 5 seconds.." 
     sleep 5 
    end 
    end 

    # ruby-smpp delegate methods 

    def mo_received(transceiver, pdu) 
    logger.info "Delegate: mo_received: from #{pdu.source_addr} to #{pdu.destination_addr}: #{pdu.short_message}" 
    end 

    def delivery_report_received(transceiver, pdu) 
    logger.info "Delegate: delivery_report_received: ref #{pdu.msg_reference} stat #{pdu.stat}" 
    end 

    def message_accepted(transceiver, mt_message_id, pdu) 
    logger.info "Delegate: message_accepted: id #{mt_message_id} smsc ref id: #{pdu.message_id}" 
    end 

    def message_rejected(transceiver, mt_message_id, pdu) 
    logger.info "Delegate: message_rejected: id #{mt_message_id} smsc ref id: #{pdu.message_id}" 
    end 

    def bound(transceiver) 
    logger.info "Delegate: transceiver bound" 
    end 

    def unbound(transceiver) 
    logger.info "Delegate: transceiver unbound" 
    EventMachine::stop_event_loop 
    end 

end 


module KeyboardHandler 
    include EventMachine::Protocols::LineText2 

    def receive_line(data) 
    sender, receiver, *body_parts = data.split 
    unless sender && receiver && body_parts.size > 0 
     puts "Syntax: <sender> <receiver> <message body>"  
    else 
     body = body_parts.join(' ') 
     puts "Sending MT from #{sender} to #{receiver}: #{body}" 
     SampleGateway.send_mt(sender, receiver, body) 
    end 
    prompt 
    end 

    def prompt 
    print "MT: " 
    $stdout.flush 
    end 
end 

/初始化

require 'eventmachine' 
require 'smpp' 

LOGFILE = Rails.root + "log/sms_gateway.log" 
Smpp::Base.logger = Logger.new(LOGFILE) 

/腳本從腳本

puts "Starting SMS Gateway. Please check the log at #{LOGFILE}" 
config = { 
    :host => '127.0.0.1', 
    :port => 6000, 
    :system_id => <SystemID>, 
    :password => <Password>, 
    :system_type => '', # default given according to SMPP 3.4 Spec 
    :interface_version => 52, 
    :source_ton => 0, 
    :source_npi => 1, 
    :destination_ton => 1, 
    :destination_npi => 1, 
    :source_address_range => '', 
    :destination_address_range => '', 
    :enquire_link_delay_secs => 10 
} 
gw = Gateway.new 
gw.start(config) 

文件/通過軌道運行亞軍

+0

檢查主機和端口設置接收的形式。 smsc與rails應用程序位於同一臺機器上嗎? – 2013-03-27 07:21:37

+0

不,我的錯誤,smsc提供個人服務,如何獲取主機和端口,再試一次 – 2013-03-27 13:59:45

+0

輸入正確的ip地址和端口,但發送俄文字符時出現問題,加上 '#coding:utf-8' 在腳本/和gateway.rb中,但它沒有幫助 – 2013-03-28 06:36:53

回答

1

問題解決了。最初被錯誤地指定了主機和端口,因爲smpp-server與RoR應用程序不在同一臺機器上。至於編碼,在俄羅斯佈局發送應

message = text.encode ("UTF-16BE"). force_encoding ("BINARY") 
Gateway.send_mt (sender, receiver, message, data_coding: 8) 

要在正確的

message = pdu.short_message.force_encoding ('UTF-16BE'). encode ('UTF-8') 
+0

嗨,我使用ruby-smpp與ussd,提交短消息編碼到UCS2時得到不可讀的符號 – 2013-04-19 10:50:39