dmacan23
3/27/2017 - 1:24 PM

MainActivity.java

package com.dmacan.sockit.general.sample;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.dmacan.sockit.core.SockIt;
import com.dmacan.sockit.io.IoSockit;
import com.dmacan.sockit.signalr.SignalRSockIt;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.net.URISyntaxException;
import java.util.Collections;

public class MainActivity extends AppCompatActivity {

    // Ove ignoriraj
    private static final String CHAT = "https://socketio-chat.now.sh/";
    private static final String EVENT_CHAT = "new message";
    private static final String PINLIST = "http://95.85.26.58/?__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript&user=55a2c87e6b12781100b55de6";
    private static final String EVENT_PINLIST = "locationUpdate";

    // Ovi su pravi
    private static final String SWIFT = "http://swiftr.azurewebsites.net/";
    private static final String EVENT_SWIFT = "broadcastMessage";

    View btnBotun;

    private SockIt sockItChat;
    private SockIt sockItPinlist;

    private SockIt sockItSwift;
    
    private Gson gson = new GsonBuilder()
            .disableHtmlEscaping()
            .setPrettyPrinting()
            .create();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        btnBotun = findViewById(R.id.btnBotun);
        setSupportActionBar(toolbar);
        initialize();
        configure(sockItSwift);
    }

    @Override
    protected void onDestroy() {
        sockItChat.disconnect();
        sockItPinlist.disconnect();
        sockItSwift.disconnect();
        super.onDestroy();
    }

    private void initialize() {
        try {
            sockItChat = new IoSockit(CHAT, Collections.singletonList(EVENT_CHAT));
            sockItPinlist = new IoSockit(PINLIST, Collections.singletonList(EVENT_PINLIST));
            sockItSwift = new SignalRSockIt(SWIFT, "ChatHub", Collections.singletonList(EVENT_SWIFT));
            btnBotun.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    send(sockItSwift);
                }
            });
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

    private void configure(SockIt sockIt) {
        sockIt.onConnectionFail(new SockIt.OnConnectionErrorListener() {
            @Override
            public void onConnectionError(Exception error) {
                Log.e("DAM", "Connection error", error);
            }
        }).onMessage(new SockIt.OnMessageListener() {
            @Override
            public void onMessage(String event, final Object message) {
                Log.i("DAM-SOCKET", event + " => " + new Gson().toJson(message));
                new Handler(Looper.getMainLooper())
                        .post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplication(), gson.toJson(message), Toast.LENGTH_SHORT).show();
                            }
                        });
            }
        }).onSent(new SockIt.OnSentListener() {
            @Override
            public void onSent(Object obj) {
                Log.i("DAM", "Sent " + new Gson().toJson(obj));
            }
        }).onSendFail(new SockIt.OnSendFailedListener() {
            @Override
            public void onSendFailed(Exception excepion) {
                Log.e("DAM", "Exception", excepion);
            }
        }).onConnectionSuccess(new SockIt.OnConnectionSuccessListener() {
            @Override
            public void onConnectionSuccess() {
                Log.i("DAM", "Connection success");
            }
        });
        sockIt.connect();
    }

    private void send(SockIt sockIt) {
        sockIt.send("send", "Mario peder");
    }
}