Created
November 11, 2013 18:41
-
-
Save ToeJamson/7418107 to your computer and use it in GitHub Desktop.
Corona SDK Multiplayer Networking API for Mobile Games
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- 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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- 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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- PUBNUB UN-SUBSCRIBE CHANNEL (STOP RECEIVING MESSAGES) | |
-- | |
multiplayer:unsubscribe({ | |
channel = "lua-corona-demo-channel" | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- 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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- 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