2

我曾考虑在我的网站上使用 PubNub。

反过来,我可以使用 PubNub 订阅频道。

但是,我需要找到一种方法来运行使用 PubNub 订阅频道的脚本。

例如,根据他们的文档http://www.pubnub.com/blog/ruby-push-api,它声明“要侦听已发布的消息,请调用订阅函数。重要的是要注意:订阅函数是阻塞的. 你会想在一个单独的进程中运行这个函数。

然后 PubNub 提供以下代码:

 ## Subscribe (Listen for Messages)
 pubnub.subscribe({
'channel ' => 'my_channel',
'callback' => lambda do |message|
    ## Message Received!!!
    puts(message['my_var']) ## print message
    return true             ## keep listening?
end
  })

我想不出一种将这个函数作为“进程”运行的方法。

没有类似背景工作的东西吗?这是我需要的吗?

感谢任何指导。

4

1 回答 1

0

The best way to get started with this (at this time) is to use the Ruby EventMachine async HTTP request method and the PubNub HTTP REST API.

  1. EventMachine::Protocols::HttpClient2 -> http://eventmachine.rubyforge.org/EventMachine/Protocols/HttpClient2.html
  2. PubNub HTTP REST API -> http://www.pubnub.com/tutorial/http-rest-push-api

    conn = EM::Protocols::HttpClient2.connect 'pubsub.pubnub.com', 80
    req  = conn.get('/subscribe/SUBSCRIBE_KEY/CHANNEL/0/TIMETOKEN')
    

Note that this is not a full example, but a good starting point... You must follow the HTTP REST Guide.

于 2011-12-06T06:25:16.590 回答