corona-usage-guide

corona icon corona usage guide

Corona Getting Started

WarpClient provides an interface for applications to interact with the cloud services. It allows client side applications to connect, subscribe and manage server side rooms, lobbies and users. The APIs are developed to be used asynchronously, which means you simply add the corresponding request listeners to the WarpClient instance to receive responses and updates. This page describes to how to set up your connection with the cloud server and introduces API usage.

In order to use the various functions available, you will have to initialize and connect the instance of WarpClient. You need to place the AppWarp SDK folder in to your top level directory and then “require” an instance of it. Keep this instance global in your application. Now initialize WarpClient by passing the apiKey and secretKey which you received after creating an app from AppHq (App42 management console).

appWarpClient = require "AppWarp.WarpClient"
appWarpClient .initialize("YourAPIKey", "YourSecretKey")

Now since AppWarp is asynchronous, it doesn’t block the main thread while waiting for responses from the server for any request that the client makes. So it is important to call the loop method on AppWarp from your application. This is important as else your application will not receive any updates. In corona for example you will need to do the following

local function gameLoop(event)
  appWarpClient.Loop()
end
Runtime:addEventListener("enterFrame", gameLoop)

This completes the setup of your project. AppWarp is designed to follow a request/response/notification model. So to connect to your game on AppWarp cloud you will need to do the following

appWarpClient.connectWithUserName("Alice")

In order to receive the callback for this request you need to implement and add the corresponding listener.

function myOnConnectDoneFunc(resultCode)
  if(resultCode == WarpResponseResultCode.SUCCESS) then
    print("Connected successfully")
  else
    print("Connect failed..")
  end
end
appWarpClient.addRequestListener("onConnectDone", myOnConnectDoneFunc)

When a response is received for a request, the SDK checks if the corresponding listener is present. If so it will be invoked. These are case sensitive as everything in Lua so ensure that the name of the listener (i.e. the first argument) you are registering is correct else the SDK will not invoke the callback.

Similarly all other APIs require a corresponding listener to be registered. Some examples

appWarpClient.addRequestListener("onJoinRoomDone", myOnJoinRoomDoneFunc)  
appWarpClient.addRequestListener("onSubscribeRoomDone", myOnSubscribeRoomDoneFunc)  
appWarpClient.joinRoom("14556235")
appWarpClient.subscribeRoom("14556235")

Notification callback functions are also similarly required to be registered by the application. Notifications are callbacks that are invoked when some activity takes place that the client has subscribed to receive. For instance if you subscribe to a room, you will get notifications when a user joins or leaves the room, sends chat messages in it etc. Following is an example of how to register for chat notifications

function mOnChatReceivedFunc(sender, message)
  print("received " .. message .. " from "..sender)
end
appWarpClient.addNotificationListener("onChatReceived", mOnChatReceivedFunc)

That’s about it. We recommend you now try out and play with the samples given.