Last active
February 12, 2016 09:26
-
-
Save wedneyyuri/d30fc41310bf4e608f47 to your computer and use it in GitHub Desktop.
Adapter for SailsJS in Socket.IO - sails.io.js in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Adaptador para conectar o Socket.IO no SailsJS - Socket Client (sails.io.js) | |
// https://github.com/socketio/socket.io-client-java | |
package br.com.example.websocket; | |
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); | |
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