发布一些消息后,Pubnub会卡住

时间:2016-08-29 13:45:49

标签: ruby-on-rails pubnub

Pubnub在发布一些消息后会卡住,它对于来自设备端的一些消息工作正常,但是在限制之后卡住了。但是在pubnub历史记录中显示相同的消息。

这是pubnub代码:

//订阅方法:

    def subscribe(channel)
        # @my_callback = lambda { |envelope| puts("----------------->"+ envelope.msg) }
        $pubnub.subscribe(channel: channel, with_presence: true)            
    end            

//Publish method:

   def publish(channel, message = {})
        # @my_callback = lambda { |envelope| puts("-------->From chat:: "+envelope.msg) }
        # pubnub.publish(:channel  => channel, :message  => msg, :callback => @my_callback )            
        puts "--------------Message---#{message}-------------------"
        $pubnub.publish(http_sync: true, channel: channel, message: message) do |envelope|
          puts envelope.status
        end

      end

 //Add listener Method:

 def add_listener(listener)
    # $pubnub.add_listener(callback: callbacks)
    $pubnub.add_listener(name: listener, callback: callbacks)
  end

 //Remove Listener Method:

  def remove_listener(listener)
    $pubnub.remove_listener(name: listener)
    # $pubnub.remove_listener(name: listener, callback: callbacks)
  end  


  //Unsubscribe Method:
  def unsubscribe(channel)
    $pubnub.unsubscribe(channel: channel) do |envelope|
      puts envelope.status
    end
  end

 //Set State Method: 
 def set_state(channel, state, uuid)
    Pubnub.logger.info("============================================================== channel : #{channel}, state : #{state}, uuid : #{uuid}") 
    $pubnub.set_state(channel: channel, state: state, uuid: uuid, http_sync: true) do |envelope|
      # puts envelope.status
    end
  end 

 //Callback Method:

  def callbacks
    callbacks = Pubnub::SubscribeCallback.new(
    message:  ->(envelope) { 
      # puts "#{envelope.result[:data]}"
      subscription_callback(envelope.result)
    },
    presence: ->(envelope) { 
      # puts #{envelope.result[:data][:message]}"
      presence_callback(envelope.result) 
    },
    status:   ->(envelope) do
      if envelope.status[:error]
        case envelope.status[:category]
          when Pubnub::Constants::STATUS_ACCESS_DENIED # :access_denied
            puts "Access Denied"
          when Pubnub::Constants::STATUS_TIMEOUT # :timeout
            puts "Timeout error"
          when Pubnub::Constants::STATUS_NON_JSON_RESPONSE # :non_json_response
            puts "Non json response"
          when Pubnub::Constants::STATUS_API_KEY_ERROR # :api_key_error
            puts "API key error"
          when Pubnub::Constants::STATUS_ERROR
            puts "Other Pubnub error"
          else
            puts "Something went wrong"
          end
        end
      end 
    ) 
  end 

0 个答案:

没有答案