Created
March 31, 2017 12:50
-
-
Save CarolusX74/2ebfa34bc53473b9bffc4c46ae5ce8ae to your computer and use it in GitHub Desktop.
Implementando ConnectionModule
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
package com.cjtp.apps.multitest.ui.fragments; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import com.cjtp.apps.multitest.R; | |
import com.cjtp.apps.multitest.utils.cjtp.connection.ConnectionModule; | |
//import com.github.nkzawa.emitter.Emitter; | |
//import com.github.nkzawa.socketio.client.IO; | |
//import com.github.nkzawa.socketio.client.Socket; | |
//import com.google.gson.JsonObject; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.net.URISyntaxException; | |
import butterknife.Bind; | |
import butterknife.ButterKnife; | |
import butterknife.OnClick; | |
import io.socket.client.IO; | |
import io.socket.client.Socket; | |
import io.socket.emitter.Emitter; | |
//import io.socket.client.IO; | |
//import io.socket.client.Socket; | |
//import io.socket.emitter.Emitter; | |
import static com.cjtp.apps.multitest.utils.Constants.MTAG; | |
/** | |
* A simple {@link Fragment} subclass. | |
* Use the {@link SocketFragment#newInstance} factory method to | |
* create an instance of this fragment. | |
*/ | |
public class SocketFragment extends Fragment implements ConnectionModule.ConnectionModuleInterface { | |
public static final String TAG = SocketFragment.class.getSimpleName(); | |
public static final String URL_SERVER = "http://104.236.96.130:8080"; | |
//-- | |
Socket socket; | |
//-- | |
final static String URL_SOCKET = /*"http://192.168.1.5:8080";*/"http://104.236.96.130:8080"; | |
final static String PING_EVENT = "PING"; | |
final static String PONG_EVENT = "PONG"; | |
final static String AUTH_EVENT = "Authentication"; | |
final static String CALL_EVENT = "CallRequest"; | |
final static String PARAM_TELEPHONE = "telefono"; | |
//--views | |
@Bind(R.id.tv_socket_status_conexion) TextView conexionTextView; | |
@Bind(R.id.tv_socket_promp) TextView prompTextView; | |
@Bind(R.id.btn_socket_connect) Button connnectButton; | |
@Bind(R.id.btn_socket_disconnect) Button disconnectButton; | |
public SocketFragment() { | |
// Required empty public constructor | |
} | |
public static SocketFragment newInstance(String param1, String param2) { | |
SocketFragment fragment = new SocketFragment(); | |
Bundle args = new Bundle(); | |
fragment.setArguments(args); | |
return fragment; | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
if (getArguments() != null) { | |
} | |
//--- | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
Log.i(TAG+MTAG,"onCreateView()*!"); | |
View view = inflater.inflate(R.layout.fragment_socket, container, false); | |
ButterKnife.bind(this, view); | |
return view; | |
} | |
//---MÉTODOS PROPIOS---------------------------------------------------------------------------- | |
public void prueba01(){ | |
Log.d(TAG+MTAG,"Prueba 01"); | |
//---init | |
IO.Options opts = new IO.Options(); | |
opts.forceNew = true; | |
opts.reconnection = false; | |
try { | |
//socket = IO.socket("http://localhost"); | |
socket = IO.socket(URL_SERVER,opts); | |
} catch (URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
Log.w(TAG+MTAG,"init ok :)"); | |
// | |
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
Log.w(TAG+MTAG,"Event_connect ok :)"); | |
//socket.emit("foo", "hi"); | |
socket.disconnect(); | |
} | |
}).on("event", new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
} | |
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
Log.w(TAG+MTAG,"Event_disconnect ok :)"); | |
} | |
}); | |
socket.connect(); | |
Log.w(TAG+MTAG,"socket.connected()? = "+String.valueOf(socket.connected())); | |
} | |
public void prueba02(){ | |
IO.Options opts = new IO.Options(); | |
opts.forceNew = true; | |
opts.reconnection = false; | |
try{ | |
/* Instance object socket */ | |
this.socket = IO.socket(URL_SOCKET, opts); | |
}catch (URISyntaxException e){ | |
e.printStackTrace(); | |
} | |
onConnectEvent("+584249013413"); | |
onDisconnectEvent(this.socket); | |
this.socket.on(PONG_EVENT, new Emitter.Listener(){ | |
@Override | |
public void call(Object... args) { | |
/* Our code */ | |
JSONObject obj = (JSONObject)args[0]; | |
Log.d(TAG+MTAG,"Recibi PONG: "+obj.toString()); | |
} | |
}); | |
this.socket.connect(); | |
} | |
public void prueba03(){ | |
Log.d(TAG+MTAG,"Prueba 03"); | |
new ConnectionModule().start(); | |
} | |
public void onSocketConnect(){ | |
//Connect to socket | |
} | |
public void onSocketDisconnect(){ | |
//Disconnect to soccket | |
} | |
//---Jesus | |
private Socket getSocket(){ | |
return this.socket; | |
} | |
private void onConnectEvent (String number){ | |
final Socket socket = getSocket(); | |
final String target = number; | |
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener(){ | |
@Override | |
public void call(Object... args) { | |
/* Our code */ | |
System.out.println("HOLAAA DESDE ON CONECT EVENTTTTTT "); | |
Log.w(TAG+MTAG,"args: "+String.valueOf(args)); | |
System.out.println("Conexion establecida"); | |
Log.w(TAG+MTAG,"args == null ? "+String.valueOf(args==null)); | |
Ping(); | |
authenticateEvent(target); | |
} | |
}); | |
} | |
private void disconnectSocket(){ | |
this.socket.disconnect(); | |
} | |
private void Ping(){ | |
final Socket socket = getSocket(); | |
JSONObject obj = new JSONObject(); | |
try { | |
obj.put("message", "PING"); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
socket.emit(PING_EVENT, obj); | |
} | |
private void authenticateEvent (String target){ | |
final Socket socket = getSocket(); | |
// Get number of the user phone | |
JSONObject obj = new JSONObject(); | |
try { | |
obj.put(PARAM_TELEPHONE, "+584249421656"); // Send own number | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
authenticateVerify(target); | |
socket.emit(AUTH_EVENT, obj); | |
} | |
private void authenticateVerify (String number){ | |
final Socket socket = getSocket(); | |
final String target = number; | |
socket.on("Verification", new Emitter.Listener(){ | |
@Override | |
public void call(Object... args) { | |
JSONObject obj = (JSONObject)args[0]; | |
System.out.println(obj.toString()); | |
if(true){ | |
callRequestEvent(target); | |
} | |
} | |
}); | |
} | |
private void callRequestEvent (String destinatario){ | |
final Socket socket = getSocket(); | |
JSONObject obj = new JSONObject(); | |
try { | |
obj.put(PARAM_TELEPHONE, destinatario); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
socket.emit(CALL_EVENT, obj); | |
} | |
private void onDisconnectEvent (Socket socket){ | |
socket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener(){ | |
@Override | |
public void call(Object... args) { | |
/* Our code */ | |
Log.d(TAG+MTAG,"Socket desconectado"+args); | |
disconnectSocket(); | |
} | |
}); | |
} | |
//---INTERFACES - LISTENERS - INTERFACES - LISTENERS - INTERFACES - LISTENERS - --------------- | |
//---socket interfaces.. | |
//--View Interfaces----View Interfaces----View Interfaces----View Interfaces----View Interfaces- | |
@OnClick(R.id.btn_socket_connect) | |
void onConnectBtnClick(Button btn){ | |
ConnectionModule.getInstance(getContext(),"+584249665551","+584167885760",this).start(); | |
//Luego de conectar (por ejemplo) se usa el listener para hacer ping... | |
} | |
@OnClick(R.id.btn_socket_disconnect) | |
void onDisconnectBtnClick(Button btn){ | |
ConnectionModule.getInstance(getContext()).stop(); | |
} | |
@OnClick(R.id.btn_socket_ping) | |
void onPingBtnClick(Button btn){ | |
//prueba01(); | |
//prueba02(); | |
//prueba03(); | |
ConnectionModule.getInstance(getContext()).pingEmission(); | |
} | |
@Override | |
public void onSocketConnected() { | |
//Pins | |
//Auth this user | |
//Send call request | |
//ConnectionModule.getInstance(getContext()).pingEmission(); | |
//conexionTextView.setText("Connected"); esto es inestable :# | |
} | |
@Override | |
public void onSocketDiscnnected() { | |
//conexionTextView.setText("Disconnected"); inestable | |
} | |
@Override | |
public void onAuthenticationVerified() { | |
} | |
@Override | |
public void onCallAccepted() { | |
//Significa que la otra persona aceptó la llamada | |
} | |
@Override | |
public void onCallRejected() { | |
//Significa que la otra persona rechazó la llamada | |
//Enviar post al servidor | |
} | |
@Override | |
public void onTextToSpeakReceived(String textToSpeak) { | |
} | |
@Override | |
public void onError() { | |
//prompTextView.setText("Error"); inestable | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment