Created
March 30, 2017 23:59
-
-
Save CarolusX74/62fceb3efcc9ed74fa3cffe5e6637ca7 to your computer and use it in GitHub Desktop.
Oystext: Prueba conexión de sockets
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
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.github.nkzawa.emitter.Emitter; | |
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.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 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 { | |
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(){ | |
Log.d(TAG+MTAG,"Prueba 02"); | |
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 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 */ | |
JSONObject obj = (JSONObject)args[0]; | |
Log.d(TAG+MTAG,"Conexion establecida"+obj.toString()); | |
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){ | |
} | |
@OnClick(R.id.btn_socket_disconnect) | |
void onDisconnectBtnClick(Button btn){ | |
} | |
@OnClick(R.id.btn_socket_ping) | |
void onPingBtnClick(Button btn){ | |
prueba01(); | |
//prueba02(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment