Skip to content

Instantly share code, notes, and snippets.

@ToeJamson
Created November 11, 2013 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ToeJamson/7418107 to your computer and use it in GitHub Desktop.
Save ToeJamson/7418107 to your computer and use it in GitHub Desktop.
Corona SDK Multiplayer Networking API for Mobile Games
require "pubnub"
--
-- GET YOUR PUBNUB KEYS HERE:
-- http://www.pubnub.com/account#api-keys
--
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
})
--
-- PUBNUB SUBSCRIBE CHANNEL (RECEIVE MESSAGES)
--
multiplayer:subscribe({
channel = "lua-corona-demo-channel",
callback = function(message)
-- MESSAGE RECEIVED!!!
print(message)
end,
errorback = function()
print("Network Connection Lost")
end
})
--
-- PUBNUB PUBLISH MESSAGE (SEND A MESSAGE)
--
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
})
--
-- PUBNUB UN-SUBSCRIBE CHANNEL (STOP RECEIVING MESSAGES)
--
multiplayer:unsubscribe({
channel = "lua-corona-demo-channel"
})
--
-- PUBNUB LOAD MESSAGE HISTORY
--
multiplayer:history({
channel = "lua-corona-demo-channel",
limit = 10,
callback = function(messages)
if not messages then
return print("ERROR LOADING HISTORY")
end
-- NO HISTORY?
if not (#messages > 0) then
return print("NO HISTORY YET")
end
-- LOOP THROUGH MESSAGE HISTORY
for i, message in ipairs(messages) do
print(Json.Encode(message))
end
end
})
--
-- PUBNUB SERVER TIME
--
multiplayer:time({
callback = function(time)
-- PRINT TIME
print("PUBNUB SERVER TIME: " .. time)
end
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment