Skip to content

Commit 41512dd

Browse files
committed
Add proxy support for WSS
1 parent 5e01965 commit 41512dd

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

build.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
<target name="clean">
2727
<delete dir="build" />
28+
<delete dir="dist" />
2829
</target>
2930

3031
</project>

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import java.util.concurrent.CountDownLatch;
4141

4242
import javax.net.ssl.SSLContext;
43-
import javax.net.ssl.SSLSocketFactory;
43+
import javax.net.ssl.SSLSocketFactory
4444

4545
import org.java_websocket.AbstractWebSocket;
4646
import org.java_websocket.WebSocket;
@@ -227,22 +227,34 @@ public void sendPing() throws NotYetConnectedException {
227227
}
228228

229229
public void run() {
230+
230231
try {
232+
boolean isNewSocket = false;
233+
231234
if( socket == null ) {
232-
if (this.uri.getScheme().equals("wss")) {
233-
SSLContext sslContext = SSLContext.getInstance("TLS");
234-
sslContext.init(null, null, null);
235-
SSLSocketFactory factory = sslContext.getSocketFactory();
236-
socket = factory.createSocket();
237-
} else {
238-
socket = new Socket( proxy );
239-
}
235+
socket = new Socket( proxy );
236+
isNewSocket = true;
237+
240238
} else if( socket.isClosed() ) {
241239
throw new IOException();
242240
}
241+
243242
socket.setTcpNoDelay( isTcpNoDelay() );
244-
if( !socket.isBound() )
243+
244+
if( !socket.isBound() ) {
245+
245246
socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout );
247+
}
248+
249+
// if the socket is set by others we don't apply any TLS wrapper
250+
if (isNewSocket && uri.getScheme().equals("wss")) {
251+
252+
SSLContext sslContext = SSLContext.getInstance("TLS");
253+
sslContext.init(null, null, null);
254+
SSLSocketFactory factory = sslContext.getSocketFactory();
255+
socket = factory.createSocket(socket, uri.getHost(), getPort(), true);
256+
}
257+
246258
istream = socket.getInputStream();
247259
ostream = socket.getOutputStream();
248260

0 commit comments

Comments
 (0)