Skip to content

Commit 115477e

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

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
import java.net.InetSocketAddress;
2+
import java.net.Proxy;
23
import java.net.URI;
34
import java.net.URISyntaxException;
4-
import java.nio.channels.ByteChannel;
5-
6-
public class ProxyClientExample extends ExampleClient {
7-
8-
public ProxyClientExample( URI serverURI , InetSocketAddress proxy ) {
9-
super( serverURI );
10-
setProxy( proxy );
11-
}
12-
13-
@Override
14-
public ByteChannel createProxyChannel( ByteChannel towrap ) {
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-
return super.createProxyChannel( towrap );
20-
}
215

6+
public class ProxyClientExample {
227
public static void main( String[] args ) throws URISyntaxException {
23-
ProxyClientExample c = new ProxyClientExample( new URI( "ws://echo.websocket.org" ), new InetSocketAddress( "proxyaddress", 80 ) );// don't forget to change "proxyaddress"
8+
ExampleClient c = new ExampleClient( new URI( "ws://echo.websocket.org" ) );
9+
c.setProxy( new Proxy( Proxy.Type.HTTP, new InetSocketAddress( "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
import javax.net.ssl.KeyManagerFactory;
99
import javax.net.ssl.SSLContext;
10+
import javax.net.ssl.SSLSocketFactory;
1011
import javax.net.ssl.TrustManagerFactory;
1112

1213
import org.java_websocket.WebSocketImpl;
13-
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
1414
import org.java_websocket.client.WebSocketClient;
1515
import org.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( new DefaultSSLWebSocketClientFactory( sslContext ) );
82+
SSLSocketFactory factory = 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
import java.nio.channels.ByteChannel;
66
import java.nio.channels.spi.AbstractSelectableChannel;
77

8+
import org.java_websocket.WebSocket.Role;
9+
810
public class SocketChannelIOHelper {
911

1012
public static boolean read( final ByteBuffer buf, WebSocketImpl ws, ByteChannel channel ) throws IOException {
@@ -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
return c != null ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
6870
}
69-
70-
public static void writeBlocking( WebSocketImpl ws, ByteChannel channel ) throws InterruptedException , IOException {
71-
assert ( channel instanceof AbstractSelectableChannel == true ? ( (AbstractSelectableChannel) channel ).isBlocking() : true );
72-
assert ( channel instanceof WrappedByteChannel == true ? ( (WrappedByteChannel) channel ).isBlocking() : true );
73-
74-
ByteBuffer buf = 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
package org.java_websocket;
22

3+
import java.net.InetSocketAddress;
4+
35
import org.java_websocket.drafts.Draft;
46
import org.java_websocket.exceptions.InvalidDataException;
7+
import org.java_websocket.exceptions.InvalidHandshakeException;
58
import org.java_websocket.framing.Framedata;
69
import org.java_websocket.framing.Framedata.Opcode;
710
import org.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-
public String getFlashPolicy( WebSocket conn ) {
87-
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + conn.getLocalSocketAddress().getPort() + "\" /></cross-domain-policy>\0";
90+
public String getFlashPolicy( WebSocket conn ) throws InvalidDataException {
91+
InetSocketAddress adr = conn.getLocalSocketAddress();
92+
if(null == adr){
93+
throw new InvalidHandshakeException( "socket not bound" );
94+
}
95+
96+
StringBuffer sb = new StringBuffer( 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+
return sb.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
public void decode( ByteBuffer socketBuffer ) {
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" : new String( 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
HandshakeState isflashedgecase = 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 ( InvalidDataException e ) {
201+
close( CloseFrame.ABNORMAL_CLOSE, "remote peer closed connection before flashpolicy could be transmitted", true );
202+
}
203203
return false;
204204
}
205205
}
@@ -315,17 +315,13 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
315315
}
316316

317317
private void decodeFrames( ByteBuffer socketBuffer ) {
318-
if( flushandclosestate )
319-
return;
320318

321319
List<Framedata> frames;
322320
try {
323321
frames = draft.translateFrame( socketBuffer );
324322
for( Framedata f : frames ) {
325323
if( DEBUG )
326324
System.out.println( "matched frame: " + f );
327-
if( flushandclosestate )
328-
return;
329325
Opcode curop = f.getOpcode();
330326
boolean fin = 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-
public String getFlashPolicy( WebSocket conn );
144+
public String getFlashPolicy( WebSocket conn ) throws InvalidDataException;
144145

145146
/** This method is used to inform the selector thread that there is data queued to be written to the socket. */
146147
public void onWriteDemand( WebSocket conn );

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)