Skip to content

Instantly share code, notes, and snippets.

@jayluxferro
Created August 30, 2018 17:08
Show Gist options
  • Save jayluxferro/aa838781157682a319e454caeae58696 to your computer and use it in GitHub Desktop.
Save jayluxferro/aa838781157682a319e454caeae58696 to your computer and use it in GitHub Desktop.
Websocket Client Java
import android.util.Log;

import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.Ack;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URI;

public class WebsocketClient {
    private static final String LOGCAT_TAG = "Websocket";
    public final Socket socket;
    private final URI uri;

    public WebsocketClient(URI uri) {
        IO.Options options = new IO.Options();
        options.reconnection = true;
        options.forceNew = true;
        //options.query = "__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=android&__sails_io_sdk_language=java";
        this.socket = IO.socket(uri, options);
        this.uri = uri;
    }

    public String id() {
        return this.socket.id();
    }

    public URI uri() {
        return this.uri;
    }

    public boolean connected() {
        return this.socket.connected();
    }

    public WebsocketClient connect() {
        if (!this.connected()) {
            this.socket.connect();
        } else {
            Log.wtf(LOGCAT_TAG, "Socket is already connected");
        }
        return this;
    }

    public WebsocketClient disconnect() {
        this.socket.disconnect();
        return this;
    }

    public Emitter post(String url, JSONObject data) {
        return request("post", url, data, null);
    }

    public Emitter post(String url, JSONObject data, Ack ack) {
        return request("post", url, data, ack);
    }

    public Emitter get(String url, Ack ack) {
        return request("get", url, null, ack);
    }

    private Emitter request(String method, String url, JSONObject data, Ack ack) {
        // TODO: Check why auto reconnect is not working
        if (!socket.connected()) {
            socket.connect();
        }

        JSONObject payload = new JSONObject();
        try {
            payload.put("method", method);
            payload.put("url", url);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (data != null) {
            try {
                payload.put("data", data);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.i(LOGCAT_TAG, "Sending " + method + " request: " + data.toString() + " to url: " + url);
        }

        if (ack == null) {
            return socket.emit(method, payload);
        } else {
            return socket.emit(method, payload, ack);
        }
    }

    public WebsocketClient on(String event, Emitter.Listener fn) {
        this.socket.on(event, fn);
        Log.i(LOGCAT_TAG, "Receiving data: " + event);
        return this;
    }

    public WebsocketClient off(String event, Emitter.Listener fn) {
        this.socket.off(event, fn);
        return this;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment