package com.nestedworld.nestedworld.api.socket.implementation;

import android.support.annotation.NonNull;

import com.nestedworld.nestedworld.api.socket.listener.ConnectionListener;
import com.nestedworld.nestedworld.api.socket.listener.SocketListener;

public class NestedWorldSocketAPI {

    private static NestedWorldSocketAPI mSingleton;
    private final SocketManager mSocketManager;

    /*
    ** Constructor
     */
    private NestedWorldSocketAPI(@NonNull final ConnectionListener connectionListener) {
        mSocketManager = new SocketManager("10.0.2.2", 2009);
        mSocketManager.setTimeOut(1000);
        mSocketManager.addSocketListener(new SocketListener() {
            @Override
            public void onSocketConnected(@NonNull SocketEvent socketEvent) {
                mSingleton = NestedWorldSocketAPI.this;
                connectionListener.OnConnectionReady(mSingleton);
            }

            @Override
            public void onSocketDisconnected(@NonNull SocketEvent socketEvent) {
                reset();
                connectionListener.OnConnectionLost();
            }

            @Override
            public void onMessageSent(@NonNull SocketEvent socketEven) {

            }

            @Override
            public void onMessageReceived(@NonNull SocketEvent socketEvent, String message) {

            }
        });
        mSocketManager.connect();
    }

    /*
    ** Singleton
     */
    public static void getInstance(@NonNull ConnectionListener connectionListener) {
        if (mSingleton == null) {
            new NestedWorldSocketAPI(connectionListener);
        }
    }

    /*
    ** Avoid leek when log out
     */
    public void reset() {
        mSingleton = null;
        mSocketManager.disconnect();
    }

    /*
    ** Public method
     */
    public void sendAttack(@NonNull String string) {
        mSocketManager.send(string);
    }
}


@@ -251,45 +251,161 @@
 er;%0A
-%0Apublic class NestedWorldSocketAPI %7B%0A
+import com.nestedworld.nestedworld.helper.log.LogHelper;%0A%0Apublic class NestedWorldSocketAPI %7B%0A%0A    private final String TAG = getClass().getSimpleName();
 %0A   
@@ -673,23 +673,31 @@
 er(%22
-10.0.2.2%22, 2009
+eip.kokakiwi.net%22, 6464
 );%0A 
@@ -733,16 +733,78 @@
 Out(1000
+0);%0A        LogHelper.d(TAG, %22Waiting for a new connection...%22
 );%0A     
@@ -954,32 +954,105 @@
  socketEvent) %7B%0A
+                LogHelper.d(TAG, %22Successfully Fully got a connection%22);%0A
                 
@@ -1267,32 +1267,87 @@
  socketEvent) %7B%0A
+                LogHelper.e(TAG, %22Connection failed%22);%0A
                 

Connect to official API