Last active
September 29, 2019 17:40
-
-
Save denizfurkan/db22f94b6fd6977a1976c044cc02cdd1 to your computer and use it in GitHub Desktop.
This file contains 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
private void websocketConnection() { | |
//Get websocket from application | |
MyApplication app = (MyApplication ) getApplication(); | |
mSocket = app.getSocket(); | |
mSocket.on(Socket.EVENT_CONNECT, onConnect); | |
mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect); | |
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError); | |
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError); | |
mSocket.on("messageFromServer", onNewLocation); | |
mSocket.connect(); | |
} | |
private Emitter.Listener onConnect = new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
runOnUiThread(() -> { | |
if (!isConnected) { | |
RequestSocket mRequestSocket = new RequestSocket(); | |
mRequestSocket.setToken("Deniz Furkan"); | |
/* your parameter */ | |
mSocket.emit("messageFromClient", new Gson().toJson(mRequestSocket)); | |
Log.i("Socket Data", new Gson().toJson(mRequestSocket)); | |
isConnected = true; | |
} | |
}); | |
} | |
}; | |
private Emitter.Listener onDisconnect = args -> runOnUiThread(() -> { | |
isConnected = false; | |
/* Toast.makeText(getApplicationContext(), | |
R.string.disconnect, Toast.LENGTH_LONG).show();*/ | |
}); | |
private Emitter.Listener onConnectError = args -> runOnUiThread(() -> { | |
/* Toast.makeText(getApplicationContext(), | |
R.string.error_connect, Toast.LENGTH_LONG).show()*/ | |
}); | |
private Emitter.Listener onNewLocation = new Emitter.Listener() { | |
@Override | |
public void call(final Object... args) { | |
runOnUiThread(() -> { | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment