我正在尝试使用Corona运行默认的pubnub Helloworld示例。电晕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提供的订阅和发布密钥
要创建API密钥,我使用了pubnub中的这些库: https://github.com/pubnub/lua
问题在于,当我为Android构建APK并运行它时,我无法在http://www.pubsub.com/console的pubsub控制台上看到这些消息。我使用给定的订阅和发布键但看不到任何内容。我获得了Internet权限的运行时错误,但后来将我的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 },
},
},
}
答案 0 :(得分:0)
请添加成功和错误回调,并在您的问题中告诉我们您获得的内容,以及来自成功或错误回调的内容。
可以找到实现成功和错误回调的示例https://github.com/pubnub/lua/blob/master/corona/examples/example-publish/main.lua#L29
geremy
答案 1 :(得分:0)
确保注册error
回调以检测您的发布呼叫事件的问题。以下是功能齐全的 pubnub.publish({...})
电话的示例。
--
-- CALL PUBLISH FUNCTION
--
function publish( channel, text )
pubnub_obj:publish({
channel = channel,
message = text,
callback = textout,
error = textout
})
end
使用此选项测试您的lua corona应用程序以成功发布消息。您将看到用于调试代码的文本输出。
--
-- PubNub : Publish Example
--
require "pubnub"
require "PubnubUtil"
textout = PubnubUtil.textout
--
-- INITIALIZE PUBNUB STATE
--
pubnub_obj = pubnub.new({
publish_key = "demo",
subscribe_key = "demo",
secret_key = nil,
ssl = nil,
origin = "pubsub.pubnub.com"
})
--
-- HIDE STATUS BAR
--
display.setStatusBar( display.HiddenStatusBar )
--
-- CALL PUBLISH FUNCTION
--
function publish(channel, text)
pubnub_obj:publish({
channel = channel,
message = text,
callback = function(r) --textout(r)
end,
error = function(r) textout(r)
end
})
end
--
-- MAIN TEST
--
local my_channel = 'lua-dsm'
--
-- Publish String
--
publish("abcd", 'Hello World!' )
--
-- Publish Dictionary Object
--
publish("efgh", { Name = 'John', Age = '25' })
--
-- Publish Array
--
publish("ijkl", { 'Sunday', 'Monday', 'Tuesday' })
在我们的“文档”页面中查看发布消息JavaScript Publish API Reference。