Skip to content

Commit 587ef3f

Browse files
committed
fixed getPort method. Additionally WebSocketFactory has now 2 specializations. One for the client and one for the server side.
1 parent 3e39cb6 commit 587ef3f

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

src/org/java_websocket/WebSocket.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public enum Role {
3030
*/
3131
public static final int DEFAULT_PORT = 80;
3232

33+
public static final int DEFAULT_WSS_PORT = 443;
34+
3335
/**
3436
* sends the closing handshake.
3537
* may be send in response to an other handshake.
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
package org.java_websocket;
22

3-
import java.io.IOException;
43
import java.net.Socket;
5-
import java.nio.channels.ByteChannel;
6-
import java.nio.channels.SelectionKey;
74
import java.util.List;
85

96
import org.java_websocket.drafts.Draft;
107

118
public interface WebSocketFactory {
129
public WebSocket createWebSocket( WebSocketAdapter a, Draft d, Socket s );
1310
public WebSocket createWebSocket( WebSocketAdapter a, List<Draft> drafts, Socket s );
14-
/**
15-
* Allows to wrap the Socketchannel( key.channel() ) to insert a protocol layer( like ssl or proxy authentication) beyond the ws layer.
16-
*
17-
* @param key
18-
* a SelectionKey of an open SocketChannel.
19-
* @return The channel on which the read and write operations will be performed.<br>
20-
*/
21-
public ByteChannel wrapChannel( SelectionKey key ) throws IOException;
11+
2212
}

src/org/java_websocket/client/WebSocketClient.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public abstract class WebSocketClient extends WebSocketAdapter implements Runnab
7272

7373
private Map<String,String> headers;
7474

75-
WebSocketFactory wf = new WebSocketFactory() {
75+
WebSocketClientFactory wf = new WebSocketClientFactory() {
7676
@Override
7777
public WebSocket createWebSocket( WebSocketAdapter a, Draft d, Socket s ) {
7878
return new WebSocketImpl( WebSocketClient.this, d, s );
@@ -84,7 +84,7 @@ public WebSocket createWebSocket( WebSocketAdapter a, List<Draft> d, Socket s )
8484
}
8585

8686
@Override
87-
public ByteChannel wrapChannel( SelectionKey c ) {
87+
public ByteChannel wrapChannel( SelectionKey c, String host, int port ) {
8888
return (ByteChannel) c.channel();
8989
}
9090
};
@@ -286,7 +286,19 @@ private final void interruptableRun() {
286286

287287
private int getPort() {
288288
int port = uri.getPort();
289-
return port == -1 ? WebSocket.DEFAULT_PORT : port;
289+
if( port == -1 ) {
290+
String scheme = uri.getScheme();
291+
if( scheme.equals( "wss" ) ) {
292+
return WebSocket.DEFAULT_WSS_PORT;
293+
}
294+
else if( scheme.equals( "ws" ) ) {
295+
return WebSocket.DEFAULT_PORT;
296+
}
297+
else{
298+
throw new RuntimeException( "unkonow scheme" + scheme );
299+
}
300+
}
301+
return port;
290302
}
291303

292304
private void finishConnect( SelectionKey key ) throws IOException , InvalidHandshakeException , InterruptedException {
@@ -296,7 +308,7 @@ private void finishConnect( SelectionKey key ) throws IOException , InvalidHands
296308
// Now that we're connected, re-register for only 'READ' keys.
297309
key.interestOps( SelectionKey.OP_READ | SelectionKey.OP_WRITE );
298310

299-
wrappedchannel = wf.wrapChannel( key );
311+
wrappedchannel = wf.wrapChannel( key, uri.getHost(), getPort() );
300312
sendHandshake();
301313
}
302314

@@ -395,7 +407,7 @@ public WebSocket getConnection() {
395407
return conn;
396408
}
397409

398-
public final void setWebSocketFactory( WebSocketFactory wsf ) {
410+
public final void setWebSocketFactory( WebSocketClientFactory wsf ) {
399411
this.wf = wsf;
400412
}
401413

@@ -410,4 +422,10 @@ public final WebSocketFactory getWebSocketFactory() {
410422
public abstract void onError( Exception ex );
411423
public void onMessage( ByteBuffer bytes ) {
412424
};
425+
426+
public interface WebSocketClientFactory extends WebSocketFactory {
427+
public ByteChannel wrapChannel( SelectionKey key, String host, int port ) throws IOException;
428+
}
413429
}
430+
431+

src/org/java_websocket/server/WebSocketServer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,5 +532,14 @@ public interface WebSocketServerFactory extends WebSocketFactory {
532532
public WebSocketImpl createWebSocket( WebSocketAdapter a, Draft d, Socket s );
533533

534534
public WebSocketImpl createWebSocket( WebSocketAdapter a, List<Draft> drafts, Socket s );
535+
536+
/**
537+
* Allows to wrap the Socketchannel( key.channel() ) to insert a protocol layer( like ssl or proxy authentication) beyond the ws layer.
538+
*
539+
* @param key
540+
* a SelectionKey of an open SocketChannel.
541+
* @return The channel on which the read and write operations will be performed.<br>
542+
*/
543+
public ByteChannel wrapChannel( SelectionKey key ) throws IOException;
535544
}
536545
}

0 commit comments

Comments
 (0)