|
40 | 40 | import java.util.concurrent.CountDownLatch; |
41 | 41 |
|
42 | 42 | import javax.net.ssl.SSLContext; |
43 | | -import javax.net.ssl.SSLSocketFactory; |
| 43 | +import javax.net.ssl.SSLSocketFactory |
44 | 44 |
|
45 | 45 | import org.java_websocket.AbstractWebSocket; |
46 | 46 | import org.java_websocket.WebSocket; |
@@ -227,22 +227,34 @@ public void sendPing() throws NotYetConnectedException { |
227 | 227 | } |
228 | 228 |
|
229 | 229 | public void run() { |
| 230 | + |
230 | 231 | try { |
| 232 | + boolean isNewSocket = false; |
| 233 | + |
231 | 234 | 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 | + |
240 | 238 | } else if( socket.isClosed() ) { |
241 | 239 | throw new IOException(); |
242 | 240 | } |
| 241 | + |
243 | 242 | socket.setTcpNoDelay( isTcpNoDelay() ); |
244 | | - if( !socket.isBound() ) |
| 243 | + |
| 244 | + if( !socket.isBound() ) { |
| 245 | + |
245 | 246 | 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 | + |
246 | 258 | istream = socket.getInputStream(); |
247 | 259 | ostream = socket.getOutputStream(); |
248 | 260 |
|
|
0 commit comments