forked from pavanu/socket-io-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient-snippet.java
133 lines (106 loc) · 4.07 KB
/
client-snippet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package <package>;
import android.app.Application;
import android.content.Context;
import android.util.Log;
import com.github.nkzawa.emitter.Emitter;
import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
/**
* Global Application Class
*/
public class ApplicationName extends Application {
private final String LOG_TAG = ApplicationName.class.getSimpleName();
private Socket socket;
@Override
public void onCreate() {
super.onCreate();
Log.d(LOG_TAG, "onCreate");
// init
getSocket();
}
@Override
public void onTerminate() {
super.onTerminate();
Log.d(LOG_TAG, "onTerminate");
}
public synchronized Socket getSocket() {
if (socket == null) {
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new SecureRandom());
IO.setDefaultSSLContext(sc);
HttpsURLConnection.setDefaultHostnameVerifier(new RelaxedHostNameVerifier());
// socket options
IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;
opts.secure = true;
opts.sslContext = sc;
socket = IO.socket("https://(hostname|ip)", opts);
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d(LOG_TAG, "Socket Connected");
}
}).on("message", new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d(LOG_TAG, "'message' event: " + args[0].toString());
}
}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d(LOG_TAG, "Socket Disconnected");
}
}).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.d(LOG_TAG, "error: " + args[0].toString());
}
});
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
}
// Connect if disconnected
if (!socket.connected()) {
Log.d(LOG_TAG, "Connecting with Socket...");
socket.connect();
} else {
Log.d(LOG_TAG, "Socket Connected");
}
return socket;
}
private 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) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
} };
public static class RelaxedHostNameVerifier implements HostnameVerifier {
public boolean verify(String hostname, SSLSession session) {
return true;
}
}
}