Skip to content

Javascript Getting Started

DhruvCShepHertz edited this page May 16, 2013 · 3 revisions

AppWarp JS SDK

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. For a complete API reference, please visit this page.

In order to use the various functions available, you will have to initialize, connect and authenticate the instance of WarpClient. WarpClient is initialized by passing the apiKey and secretKey which you received after creating an app from AppHq (App42 management console). If you are not a registered App42 user, you can sign up here.

    <script src="AppWarpClientSDK.js"></script>
    <script>
        WarpClient.initialize("Your API Key", "Your Secret Key");
    </script>

After initialization, it is recommended that you implement and add a request listeners which will be called by WarpClient with the result of the requests. WarpClient comprises different listeners for different type of requests namely

  • Connection Request Listener
  • Lobby Request Listener
  • Zone Request Listener
  • Room Request Listener
  • Chat Request Listener
  • Update Request Listener
  • Notification Listener
    <script src="AppWarpClientSDK.js"></script>
    <script>
        WarpClient.initialize("Your API Key", "Your Secret Key");
        WarpClient.addConnectionRequestListener(new MyConnectionRequestListener());
        WarpClient.addLobbyRequestListener(new MyLobbyRequestListener());
        WarpClient.addZoneRequestListener(new MyZoneRequestListener());
        WarpClient.addRoomRequestListener(new MyRoomRequestListener());
        WarpClient.addChatRequestListener(new MyChatRequestListener());
        WarpClient.addUpdateRequestListener(new MyUpdateRequestListener());
        WarpClient.addNotificationListener(new MyNotificationListener());

        function MyConnectionRequestListener() {
                this.onConnectDone = function(result){
                };
		
                this.onJoinZoneDone = function(result){
                };
		
                this.onDisconnectDone = function(result){
                };	
        }

        function MyLobbyRequestListener(){
                this.onJoinLobbyDone = function(event){
                }
                this.onLeaveLobbyDone = function(event){					
                };
                this.onSubscribeLobbyDone = function(event){
                };
                this.onUnsubscribeLobbyDone = function(event){
                };
                this.onGetLiveLobbyInfoDone = function(event){
                };
        }

        function MyZoneRequestListener() {
                this.onCreateRoomDone = function(event){
                };
                this.onDeleteRoomDone = function(event){
                };
                this.onGetAllRoomsDone = function(event){
                };
                this.onGetOnlineUsersDone = function(event){
                };
                this.onGetLiveUserInfoDone = function(event){
                };
                this.onSetCustomUserInfoDone = function(event){
                };	
        }

        function MyRoomRequestListener() {
                this.onSubscribeRoomDone = function(event){
                };
                this.onUnsubscribeRoomDone = function(event){
                };
                this.onJoinRoomDone = function(event){
                };
                this.onLeaveRoomDone = function(event){
                };
                this.onGetLiveRoomInfoDone = function(event){	
                };
                this.onSetCustomRoomDataDone = function(event){
                };	
        }

        function MyChatRequestListener() {
                this.onSendChatDone = function(result){
                };	
        }

        function MyUpdateRequestListener() {
                this.onSendUpdateDone = function(result){
                };
        }

        function MyNotificationListener() {
                this.onRoomCreated = function(roomdata){
                };
                this.onRoomDestroyed = function(roomdata){
                };
                this.onUserLeftRoom = function(roomdata, user){
                };
                this.onUserJoinedRoom = function(roomdata, user){
                };
                this.onUserLeftLobby = function(lobbydata, user){
                };
                this.onUserJoinedLobby = function(lobbydata, user){
                };
                this.onChatReceived = function(chatevent){
                };
                this.onUpdatePeersReceived = function(updateevent){
                };	
        }
    </script>

Once you have supplied all the listeners you have to call the connect() method of WarpClient to connect to the AppWarp servers

        WarpClient.initialize("Your API Key", "Your Secret Key");
        WarpClient.addConnectionRequestListener(new MyConnectionRequestListener());
        WarpClient.addLobbyRequestListener(new MyLobbyRequestListener());
        WarpClient.addZoneRequestListener(new MyZoneRequestListener());
        WarpClient.addRoomRequestListener(new MyRoomRequestListener());
        WarpClient.addChatRequestListener(new MyChatRequestListener());
        WarpClient.addUpdateRequestListener(new MyUpdateRequestListener());
        WarpClient.addNotificationListener(new MyNotificationListener());

        WarpClient.connect();

Result Codes : These can be retrieved when an event callback is invoked from the event instance.

    byte SUCCESS = 0;    
    byte AUTH_ERROR = 1;    
    byte RESOURCE_NOT_FOUND = 2;    
    byte RESOURCE_MOVED = 3;     
    byte BAD_REQUEST = 4;
    byte CONNECTION_ERR = 5;
    byte UNKNOWN_ERROR = 6;

After successfully connecting to AppWarp, you are required to call the joinZone() method of WarpClient providing a username as parameter

WarpClient.joinZone(username)

Once you have successfully joined you can either join the lobby or join a particular room. You should also subscribe the lobby or the room joined to catch the notifications generated in that particular room or lobby.

WarpClient.joinRoom(roomId);
WarpClient.subscribeRoom(roomID);

To exchanges messages you can either send messages in plain text format using sendChat() method or send messages in binary format using sendUpdate() method respectively. To catch the messages send through sendChat() method use the onChatReceived() method of MyNotificationListener and to catch the messages send through onUpdatePeersReceived() method of MyNotificationListener. You can also exchange messages in JSON by simply using JSON.stringify() and JSON.parse() methods to transform messages into strings and then sending them through sendChat() method of WarpClient.

var msg = {
    action: 'move',
    pos_x: 120,
    pos_y: 200
};

WarpClient.sendChat(JSON.stringify(msg));

Appwarp also allows you to set custom data for users and rooms through the methods setCustomUserData() and setCustomRoomData(). To retrieve the custom data and other information WarpClient comprises getLiveUserInfo() and getLiveRoomInfo() methods.

Clone this wiki locally