这是一个基于简单的Corona SDK示例 - 无法通过pubnub控制台进行订阅。

我正在使用 Corona 运行默认的 pubnub HelloWorld 示例。在模拟器中,Corona SDK 程序运行正常,但是由于某种原因,我无法在 pubnub 控制台中看到输出。以下是用于 pubnub Corona SDK 的源代码。

require "pubnub"

multiplayer = pubnub.new({
    publish_key   = "my_publish_key",
    subscribe_key = "my_subscribe_key",
    secret_key    = nil,
    ssl           = nil,
    origin        = "pubsub.pubnub.com"
})

multiplayer:subscribe({
    channel  = "hello-world-corona",
    callback = function(message)
      print(message.msgtext)
    end,
    errorback = function()
      print("Oh no!!! Dropped 3G Conection!")
    end
})

function send_a_message(text)
  multiplayer:publish({
     channel = "hello-world-corona",
     message = { msgtext = text }
   })
end

function send_hello_world()
  send_a_message("Hello World!!!")
end

timer.performWithDelay( 500, send_hello_world, 10 )
send_hello_world()

我使用的是 pubnub 提供的 subscribe 和 publish 密钥。创建 API 密钥时,我使用了 pubnub 的这些库:https://github.com/pubnub/lua

问题在于,当我为 Android 构建 APK 并运行它时,我无法在 http://www.pubsub.com/console 控制台上看到消息。我使用了给定的 subscribe 和 publish 密钥,但是什么都没有显示。我曾经因为没有互联网权限而出现运行时错误,但是后来将我的 build.settings 改为以下内容后就不再出现运行时错误了:

build.settings:

settings =
{
android =
   {
    permissions =
        {
            { name = ".permission.C2D_MESSAGE", protectionLevel = "signature" },
        },
        usesPermissions =
        {
           -- Required by the MapView to fetch its contents from the Google Maps
           --servers.
            "android.permission.INTERNET",
            "android.permission.GET_ACCOUNTS",
            "android.permission.RECEIVE_BOOT_COMPLETED",
            "com.google.android.c2dm.permission.RECEIVE",
            ".permission.C2D_MESSAGE",

          -- Optional permission used to display current location via the GPS.
            "android.permission.ACCESS_FINE_LOCATION",

          -- Optional permission used to display current location via WiFi or
          -- cellular
          -- service.
          "android.permission.ACCESS_COARSE_LOCATION",
      },
      usesFeatures =
      {
          -- If you set permissions "ACCESS_FINE_LOCATION" and
          --"ACCESS_COARSE_LOCATION" above, then you may want to set up
          --your app to not require location services as follows.
          -- Otherwise, devices that do not have location sevices (such as a GPS) will
          -- be unable
          -- to purchase this app in the app store.
            { name = "android.hardware.location", required = false },
            { name = "android.hardware.location.gps", required = false },
            { name = "android.hardware.location.network", required = false },
        },
   },
}
点赞
用户970193
用户970193

请添加成功和错误回调,并且在你的问题中告诉我们你得到了什么,以及它是来自成功还是错误回调。

实现成功和错误回调的示例可以在https://github.com/pubnub/lua/blob/master/corona/examples/example-publish/main.lua#L29找到。

2014-08-29 19:51:55
用户524733
用户524733

PubNub Corona SDK 调试发布事件

请确保注册 error 回调来检测您的发布调用事件是否存在问题。下面是一个完全功能的 pubnub.publish({...}) 调用的示例。

--
-- 调用发布功能
--
function publish(channel, text)
    pubnub_obj:publish({
        channel = channel,
        message = text,
        callback = textout,
        error = textout
    })
end

完整的 Lua 发布演示示例:

使用此示例来测试 Lua Corona 应用程序以成功发布消息。您将看到文本输出以调试代码。

--
-- PubNub:发布示例
--

require "pubnub"
require "PubnubUtil"

textout = PubnubUtil.textout

--
-- 初始化 PubNub 状态
--
pubnub_obj = pubnub.new({
    publish_key   = "demo",
    subscribe_key = "demo",
    secret_key    = nil,
    ssl           = nil,
    origin        = "pubsub.pubnub.com"
})

--
-- 隐藏状态栏
--
display.setStatusBar(display.HiddenStatusBar)

--
-- 调用发布功能
--
function publish(channel, text)
    pubnub_obj:publish({
        channel = channel,
        message = text,
        callback = function(r) --textout(r)
        end,
        error = function(r) textout(r)
        end
    })
end

--
-- 主要测试
--
local my_channel = 'lua-dsm'

--
-- 发布字符串
--
publish("abcd", 'Hello World!')

--
-- 发布字典对象
--
publish("efgh", { Name = 'John', Age = '25' })

--
-- 发布数组
--
publish("ijkl", { 'Sunday', 'Monday', 'Tuesday' })

在我们的文档页面中查看使用 JavaScript 发布 API 参考 发布消息。

2014-08-29 20:11:42