Skip to content

Commit 7fa9e95

Browse files
docs: add SSL examples
1 parent d97f457 commit 7fa9e95

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/site/markdown/initialization.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,37 @@ socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
266266
});
267267
```
268268

269+
#### `callFactory`
270+
271+
The [OkHttpClient instance](https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/) to use for HTTP long-polling requests.
272+
273+
```java
274+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
275+
.readTimeout(1000, TimeUnit.MILLISECONDS)
276+
.writeTimeout(1000, TimeUnit.MILLISECONDS)
277+
.build();
278+
279+
IO.Options options = new IO.Options();
280+
options.callFactory = okHttpClient;
281+
282+
Socket socket = IO.socket(URI.create("https://example.com"), options);
283+
```
284+
285+
#### `webSocketFactory`
286+
287+
The [OkHttpClient instance](https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/) to use for WebSocket connections.
288+
289+
```java
290+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
291+
.minWebSocketMessageToCompress(2048)
292+
.build();
293+
294+
IO.Options options = new IO.Options();
295+
options.webSocketFactory = okHttpClient;
296+
297+
Socket socket = IO.socket(URI.create("https://example.com"), options);
298+
```
299+
269300
### Socket options
270301

271302
These settings are specific to the given Socket instance.
@@ -314,3 +345,84 @@ Or manually force the Socket instance to reconnect:
314345
options.auth.put("token", "efgh");
315346
socket.disconnect().connect();
316347
```
348+
349+
## SSL connections
350+
351+
### With a keystore
352+
353+
```java
354+
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
355+
public boolean verify(String hostname, SSLSession sslSession) {
356+
return hostname.equals("example.com");
357+
}
358+
};
359+
360+
KeyStore ks = KeyStore.getInstance("JKS");
361+
File file = new File("path/to/the/keystore.jks");
362+
ks.load(new FileInputStream(file), "password".toCharArray());
363+
364+
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
365+
kmf.init(ks, "password".toCharArray());
366+
367+
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
368+
tmf.init(ks);
369+
370+
SSLContext sslContext = SSLContext.getInstance("TLS");
371+
sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
372+
373+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
374+
.hostnameVerifier(hostnameVerifier)
375+
.sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) tmf.getTrustManagers()[0])
376+
.build();
377+
378+
IO.Options options = new IO.Options();
379+
options.callFactory = okHttpClient;
380+
options.webSocketFactory = okHttpClient;
381+
382+
Socket socket = IO.socket(URI.create("https://example.com"), options);
383+
```
384+
385+
### Trust all certificates
386+
387+
Please use with caution, as this defeats the whole purpose of using secure connections.
388+
389+
This is equivalent to `rejectUnauthorized: false` for the JavaScript client.
390+
391+
```java
392+
HostnameVerifier hostnameVerifier = new HostnameVerifier() {
393+
@Override
394+
public boolean verify(String hostname, SSLSession sslSession) {
395+
return true;
396+
}
397+
};
398+
399+
X509TrustManager trustManager = new X509TrustManager() {
400+
public X509Certificate[] getAcceptedIssuers() {
401+
return new X509Certificate[] {};
402+
}
403+
404+
@Override
405+
public void checkClientTrusted(X509Certificate[] arg0, String arg1) {
406+
// not implemented
407+
}
408+
409+
@Override
410+
public void checkServerTrusted(X509Certificate[] arg0, String arg1) {
411+
// not implemented
412+
}
413+
};
414+
415+
SSLContext sslContext = SSLContext.getInstance("TLS");
416+
sslContext.init(null, new TrustManager[] { trustManager }, null);
417+
418+
OkHttpClient okHttpClient = new OkHttpClient.Builder()
419+
.hostnameVerifier(hostnameVerifier)
420+
.sslSocketFactory(sslContext.getSocketFactory(), trustManager)
421+
.build();
422+
423+
IO.Options options = new IO.Options();
424+
options.callFactory = okHttpClient;
425+
options.webSocketFactory = okHttpClient;
426+
427+
Socket socket = IO.socket(URI.create("https://example.com"), options);
428+
```

0 commit comments

Comments
 (0)