使用pubnub与corona sdk - 基本配置

时间:2014-08-20 20:06:54

标签: corona pubnub

我一直在使用corona sdk一段时间,并希望开始使用我的多用户应用程序实现pubnub。

我下载了lua-master zip文件并将其解压缩。我的电晕应用程序总监需要哪些文件和文件夹才能开始测试? - 谢谢

1 个答案:

答案 0 :(得分:0)

PubNub Corona Lua SDK

访问开始: https://github.com/pubnub/lua/tree/master/corona

用于构建和扩展多人移动游戏的PubNub数据流网络。游戏状态变化,玩家移动,位置感知和监控。

PubNub Corona Lua

务必将 "pubnub.lua" 复制到您的项目目录中,

并查看3.3目录中的示例代码以获取完整的代码示例!

  

pubnub.lua - > https://github.com/pubnub/lua/blob/master/corona/pubnub.lua

初始化

require "pubnub"

multiplayer = pubnub.new({
    publish_key   = "demo",             -- YOUR PUBLISH KEY
    subscribe_key = "demo",             -- YOUR SUBSCRIBE KEY
    secret_key    = nil,                -- YOUR SECRET KEY
    ssl           = nil,                -- ENABLE SSL?
    origin        = "pubsub.pubnub.com" -- PUBNUB CLOUD ORIGIN
})

发布

multiplayer:publish({
    channel  = "lua-corona-demo-channel",
    message  = { "1234", 2, 3, 4 },
    callback = function(info)

        -- WAS MESSAGE DELIVERED?
        if info[1] then
            print("MESSAGE DELIVERED SUCCESSFULLY!")
        else
            print("MESSAGE FAILED BECAUSE -> " .. info[2])
        end

    end
})

订阅

multiplayer:subscribe({
    channel  = "lua-corona-demo-channel",
    callback = function(message)
        -- MESSAGE RECEIVED!!!
        print(Json.Encode(message))
    end,
    errorback = function()
        print("Network Connection Lost")
    end
})

取消

multiplayer:unsubscribe({
    channel = "lua-corona-demo-channel"
})

历史

function detailedHistory(channel, count, reverse)
    pubnub_obj:detailedHistory({
        channel = channel,
        count = count,
        reverse = reverse,
        callback = function(response)
            if response then
                for k, v in pairs(response[1])
                    do
                    print( type (v) )
                    if (type (v) == 'string')
                    then print(v)
                    elseif (type (v) == 'table')
                    then
                        for i,line in ipairs(v) do
                            print(line)
                        end
                    end
                end
            end

        end
    })
end

local my_channel = 'hello_world'
detailedHistory( my_channel, 5, false )