Skip to content

Commit 115477e

Browse files
committed
Merge pull request TooTallNate#205 from Davidiusdadi/master
made websocket client use java.net instead of java.nio
2 parents 77e1fca + 15f49ed commit 115477e

File tree

11 files changed

+235
-381
lines changed

11 files changed

+235
-381
lines changed
Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
importjava.net.InetSocketAddress;
2+
importjava.net.Proxy;
23
importjava.net.URI;
34
importjava.net.URISyntaxException;
4-
importjava.nio.channels.ByteChannel;
5-
6-
publicclassProxyClientExampleextendsExampleClient{
7-
8-
publicProxyClientExample( URIserverURI , InetSocketAddressproxy ){
9-
super( serverURI );
10-
setProxy( proxy );
11-
}
12-
13-
@Override
14-
publicByteChannelcreateProxyChannel( ByteChanneltowrap ){
15-
/*
16-
* You can create custom proxy handshake here.
17-
* For more infos see: WebSocketClient.DefaultClientProxyChannel and http://tools.ietf.org/html/rfc6455#section-4.1
18-
*/
19-
returnsuper.createProxyChannel( towrap );
20-
}
215

6+
publicclassProxyClientExample{
227
publicstaticvoidmain( String[] args ) throwsURISyntaxException{
23-
ProxyClientExamplec = newProxyClientExample( newURI( "ws://echo.websocket.org" ), newInetSocketAddress( "proxyaddress", 80 ) );// don't forget to change "proxyaddress"
8+
ExampleClientc = newExampleClient( newURI( "ws://echo.websocket.org" ) );
9+
c.setProxy( newProxy( Proxy.Type.HTTP, newInetSocketAddress( "proxyaddress", 80 ) ) );
2410
c.connect();
2511
}
2612
}

‎src/main/example/SSLClientExample.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88
importjavax.net.ssl.KeyManagerFactory;
99
importjavax.net.ssl.SSLContext;
10+
importjavax.net.ssl.SSLSocketFactory;
1011
importjavax.net.ssl.TrustManagerFactory;
1112

1213
importorg.java_websocket.WebSocketImpl;
13-
importorg.java_websocket.client.DefaultSSLWebSocketClientFactory;
1414
importorg.java_websocket.client.WebSocketClient;
1515
importorg.java_websocket.handshake.ServerHandshake;
1616

@@ -79,7 +79,9 @@ public static void main( String[] args ) throws Exception{
7979
sslContext.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
8080
// sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates
8181

82-
chatclient.setWebSocketFactory( newDefaultSSLWebSocketClientFactory( sslContext ) );
82+
SSLSocketFactoryfactory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();
83+
84+
chatclient.setSocket( factory.createSocket() );
8385

8486
chatclient.connectBlocking();
8587

‎src/main/java/org/java_websocket/SocketChannelIOHelper.java‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
importjava.nio.channels.ByteChannel;
66
importjava.nio.channels.spi.AbstractSelectableChannel;
77

8+
importorg.java_websocket.WebSocket.Role;
9+
810
publicclassSocketChannelIOHelper{
911

1012
publicstaticbooleanread( finalByteBufferbuf, WebSocketImplws, ByteChannelchannel ) throwsIOException{
@@ -59,21 +61,11 @@ public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws
5961
} while ( buffer != null );
6062
}
6163

62-
if( ws.outQueue.isEmpty() && ws.isFlushAndClose() /*&& ( c == null || c.isNeedWrite() )*/){
64+
if( ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft().getRole() == Role.SERVER ){//
6365
synchronized ( ws ){
6466
ws.closeConnection();
6567
}
6668
}
6769
returnc != null ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
6870
}
69-
70-
publicstaticvoidwriteBlocking( WebSocketImplws, ByteChannelchannel ) throwsInterruptedException , IOException{
71-
assert ( channelinstanceofAbstractSelectableChannel == true ? ( (AbstractSelectableChannel) channel ).isBlocking() : true );
72-
assert ( channelinstanceofWrappedByteChannel == true ? ( (WrappedByteChannel) channel ).isBlocking() : true );
73-
74-
ByteBufferbuf = ws.outQueue.take();
75-
while ( buf.hasRemaining() )
76-
channel.write( buf );
77-
}
78-
7971
}

‎src/main/java/org/java_websocket/WebSocketAdapter.java‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
packageorg.java_websocket;
22

3+
importjava.net.InetSocketAddress;
4+
35
importorg.java_websocket.drafts.Draft;
46
importorg.java_websocket.exceptions.InvalidDataException;
7+
importorg.java_websocket.exceptions.InvalidHandshakeException;
58
importorg.java_websocket.framing.Framedata;
69
importorg.java_websocket.framing.Framedata.Opcode;
710
importorg.java_websocket.framing.FramedataImpl1;
@@ -79,12 +82,23 @@ public void onWebsocketPong( WebSocket conn, Framedata f ){
7982
* This is specifically implemented for gitime's WebSocket client for Flash:
8083
* http://github.com/gimite/web-socket-js
8184
*
82-
* @return An XML String that comforms to Flash's security policy. You MUST
85+
* @return An XML String that comforts to Flash's security policy. You MUST
8386
* not include the null char at the end, it is appended automatically.
87+
* @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained e.g because the websocket is not connected.
8488
*/
8589
@Override
86-
publicStringgetFlashPolicy( WebSocketconn ){
87-
return"<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + conn.getLocalSocketAddress().getPort() + "\" /></cross-domain-policy>\0";
90+
publicStringgetFlashPolicy( WebSocketconn ) throwsInvalidDataException{
91+
InetSocketAddressadr = conn.getLocalSocketAddress();
92+
if(null == adr){
93+
thrownewInvalidHandshakeException( "socket not bound" );
94+
}
95+
96+
StringBuffersb = newStringBuffer( 90 );
97+
sb.append( "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" );
98+
sb.append(adr.getPort());
99+
sb.append( "\" /></cross-domain-policy>\0" );
100+
101+
returnsb.toString();
88102
}
89103

90104
}

‎src/main/java/org/java_websocket/WebSocketImpl.java‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,11 @@ public WebSocketImpl( WebSocketListener listener , List<Draft> drafts , Socket s
151151
publicvoiddecode( ByteBuffersocketBuffer ){
152152
assert ( socketBuffer.hasRemaining() );
153153

154-
if( flushandclosestate ){
155-
return;
156-
}
157-
158154
if( DEBUG )
159155
System.out.println( "process(" + socketBuffer.remaining() + "):{" + ( socketBuffer.remaining() > 1000 ? "too big to display" : newString( socketBuffer.array(), socketBuffer.position(), socketBuffer.remaining() ) ) + "}" );
160156

161-
if( readystate==READYSTATE.OPEN ){
162-
decodeFrames( socketBuffer );
157+
if( readystate!=READYSTATE.NOT_YET_CONNECTED ){
158+
decodeFrames( socketBuffer );;
163159
} else{
164160
if( decodeHandshake( socketBuffer ) ){
165161
assert ( tmpHandshakeBytes.hasRemaining() != socketBuffer.hasRemaining() || !socketBuffer.hasRemaining() ); // the buffers will never have remaining bytes at the same time
@@ -198,8 +194,12 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ){
198194
if( draft == null ){
199195
HandshakeStateisflashedgecase = isFlashEdgeCase( socketBuffer );
200196
if( isflashedgecase == HandshakeState.MATCHED ){
201-
write( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
202-
close( CloseFrame.FLASHPOLICY, "" );
197+
try{
198+
write( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
199+
close( CloseFrame.FLASHPOLICY, "" );
200+
} catch ( InvalidDataExceptione ){
201+
close( CloseFrame.ABNORMAL_CLOSE, "remote peer closed connection before flashpolicy could be transmitted", true );
202+
}
203203
returnfalse;
204204
}
205205
}
@@ -315,17 +315,13 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ){
315315
}
316316

317317
privatevoiddecodeFrames( ByteBuffersocketBuffer ){
318-
if( flushandclosestate )
319-
return;
320318

321319
List<Framedata> frames;
322320
try{
323321
frames = draft.translateFrame( socketBuffer );
324322
for( Framedataf : frames ){
325323
if( DEBUG )
326324
System.out.println( "matched frame: " + f );
327-
if( flushandclosestate )
328-
return;
329325
Opcodecurop = f.getOpcode();
330326
booleanfin = f.isFin();
331327

‎src/main/java/org/java_websocket/WebSocketListener.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ public interface WebSocketListener{
139139
/**
140140
* Gets the XML string that should be returned if a client requests a Flash
141141
* security policy.
142+
* @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained.
142143
*/
143-
publicStringgetFlashPolicy( WebSocketconn );
144+
publicStringgetFlashPolicy( WebSocketconn )throwsInvalidDataException;
144145

145146
/** This method is used to inform the selector thread that there is data queued to be written to the socket. */
146147
publicvoidonWriteDemand( WebSocketconn );

‎src/main/java/org/java_websocket/client/DefaultSSLWebSocketClientFactory.java‎

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎src/main/java/org/java_websocket/client/DefaultWebSocketClientFactory.java‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
(0)