Skip to content

Instantly share code, notes, and snippets.

@vivek1794
Created February 4, 2018 17:08
Show Gist options
  • Save vivek1794/ccb0fc4cd2939b3c059e59b02086bf9c to your computer and use it in GitHub Desktop.
Save vivek1794/ccb0fc4cd2939b3c059e59b02086bf9c to your computer and use it in GitHub Desktop.
class SignallingClient {
private static SignallingClient instance;
private String roomName = null;
private Socket socket;
boolean isChannelReady = false;
boolean isInitiator = false;
boolean isStarted = false;
private SignalingInterface callback;
//This piece of code should not go into production!!
//This will help in cases where the node server is running in non-https server and you want to ignore the warnings
@SuppressLint("TrustAllX509TrustManager")
private final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
public void checkClientTrusted(X509Certificate[] chain,
String authType) {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) {
}
}};
public static SignallingClient getInstance() {
if (instance == null) {
instance = new SignallingClient();
}
if (instance.roomName == null) {
//set the room name here
instance.roomName = "some_room_name";
}
return instance;
}
public void init(SignalingInterface signalingInterface) {
this.callback = signalingInterface;
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, trustAllCerts, null);
IO.setDefaultHostnameVerifier((hostname, session) -> true);
IO.setDefaultSSLContext(sslcontext);
//set the socket.io url here
socket = IO.socket("your_socket_io_instance_url_with_port");
socket.connect();
Log.d("SignallingClient", "init() called");
if (!roomName.isEmpty()) {
emitInitStatement(roomName);
}
//room created event.
socket.on("created", args -> {
Log.d("SignallingClient", "created call() called with: args = [" + Arrays.toString(args) + "]");
isInitiator = true;
callback.onCreatedRoom();
});
//room is full event
socket.on("full", args -> Log.d("SignallingClient", "full call() called with: args = [" + Arrays.toString(args) + "]"));
//peer joined event
socket.on("join", args -> {
Log.d("SignallingClient", "join call() called with: args = [" + Arrays.toString(args) + "]");
isChannelReady = true;
callback.onNewPeerJoined();
});
//when you joined a chat room successfully
socket.on("joined", args -> {
Log.d("SignallingClient", "joined call() called with: args = [" + Arrays.toString(args) + "]");
isChannelReady = true;
callback.onJoinedRoom();
});
//log event
socket.on("log", args -> Log.d("SignallingClient", "log call() called with: args = [" + Arrays.toString(args) + "]"));
//bye event
socket.on("bye", args -> callback.onRemoteHangUp((String) args[0]));
//messages - SDP and ICE candidates are transferred through this
socket.on("message", args -> {
Log.d("SignallingClient", "message call() called with: args = [" + Arrays.toString(args) + "]");
if (args[0] instanceof String) {
Log.d("SignallingClient", "String received :: " + args[0]);
String data = (String) args[0];
if (data.equalsIgnoreCase("got user media")) {
callback.onTryToStart();
}
if (data.equalsIgnoreCase("bye")) {
callback.onRemoteHangUp(data);
}
} else if (args[0] instanceof JSONObject) {
try {
JSONObject data = (JSONObject) args[0];
Log.d("SignallingClient", "Json Received :: " + data.toString());
String type = data.getString("type");
if (type.equalsIgnoreCase("offer")) {
callback.onOfferReceived(data);
} else if (type.equalsIgnoreCase("answer") && isStarted) {
callback.onAnswerReceived(data);
} else if (type.equalsIgnoreCase("candidate") && isStarted) {
callback.onIceCandidateReceived(data);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} catch (URISyntaxException | NoSuchAlgorithmException | KeyManagementException e) {
e.printStackTrace();
}
}
private void emitInitStatement(String message) {
Log.d("SignallingClient", "emitInitStatement() called with: event = [" + "create or join" + "], message = [" + message + "]");
socket.emit("create or join", message);
}
public void emitMessage(String message) {
Log.d("SignallingClient", "emitMessage() called with: message = [" + message + "]");
socket.emit("message", message);
}
public void emitMessage(SessionDescription message) {
try {
Log.d("SignallingClient", "emitMessage() called with: message = [" + message + "]");
JSONObject obj = new JSONObject();
obj.put("type", message.type.canonicalForm());
obj.put("sdp", message.description);
Log.d("emitMessage", obj.toString());
socket.emit("message", obj);
Log.d("vivek1794", obj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
public void emitIceCandidate(IceCandidate iceCandidate) {
try {
JSONObject object = new JSONObject();
object.put("type", "candidate");
object.put("label", iceCandidate.sdpMLineIndex);
object.put("id", iceCandidate.sdpMid);
object.put("candidate", iceCandidate.sdp);
socket.emit("message", object);
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
socket.emit("bye", roomName);
socket.disconnect();
socket.close();
}
interface SignalingInterface {
void onRemoteHangUp(String msg);
void onOfferReceived(JSONObject data);
void onAnswerReceived(JSONObject data);
void onIceCandidateReceived(JSONObject data);
void onTryToStart();
void onCreatedRoom();
void onJoinedRoom();
void onNewPeerJoined();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment