首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Ruby on Rails中使用Smpp

在Ruby on Rails中使用Smpp
EN

Stack Overflow用户
提问于 2013-03-27 14:30:26
回答 1查看 2.1K关注 0票数 1

需要通过Smpp发送和接收短信,注册号,接收的systemID和密码,但无法连接。连接到项目gem 'ruby-smpp',以getaway系统为例,它只更改了​​this it和password的值。

在日志中:

代码语言:javascript
运行
复制
<- (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

在控制台中:

代码语言:javascript
运行
复制
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地址,但是服务器和本地机器上的日志是相同的。

代码语言:javascript
运行
复制
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

/initializers

代码语言:javascript
运行
复制
require 'eventmachine'
require 'smpp'

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

/script

代码语言:javascript
运行
复制
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)

脚本中的文件/运行rails运行器

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-02 16:57:07

问题解决了。最初指定的主机和端口不正确,因为smpp-server与RoR-application不在同一台机器上。至于编码,用俄文布局发送应该是

代码语言:javascript
运行
复制
message = text.encode ("UTF-16BE"). force_encoding ("BINARY")
Gateway.send_mt (sender, receiver, message, data_coding: 8)

在右侧接收表单

代码语言:javascript
运行
复制
message = pdu.short_message.force_encoding ('UTF-16BE'). encode ('UTF-8')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15653061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档