加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Pubnub发布不发布

发布时间:2020-12-17 03:24:45 所属栏目:百科 来源:网络整理
导读:我试图向pubnub频道发布消息,但它不在服务器上发布.然而它在本地机器上工作得非常好.知道我做错了吗? class Message ActiveRecord::Base after_create :send_message_to_driver def send_message_to_ABC $pubnub.publish( channel: "chat_ABC",message: mes
我试图向pubnub频道发布消息,但它不在服务器上发布.然而它在本地机器上工作得非常好.知道我做错了吗?

class Message < ActiveRecord::Base

  after_create :send_message_to_driver

  def send_message_to_ABC
    $pubnub.publish(
        channel: "chat_ABC",message: message
    ) do |env|
      puts env.parsed_response
    end
  end
end

$pubnub正在为app全局初始化.

解决方法

好的一些研究和调试我能够解决这个问题.出现此问题的原因是,默认情况下,Ruby操作是异步的.因此脚本在发布完成之前终止.幸运的是,我们为这个pubnub的发布方法提供了一个选项http_sync.将其设置为true可确保在发布完成之前不终止该流.所以新代码就是

$pubnub.publish(
  http_sync: true,channel: "chat_ABC",message: message
) do |env|
  puts env.parsed_response
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读