Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Forked from ToeJamson/1.lua
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenlb/10023637 to your computer and use it in GitHub Desktop.
Save stephenlb/10023637 to your computer and use it in GitHub Desktop.
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(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