Skip to content

made websocket client use java.net instead of java.nio #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 23, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions src/main/example/ProxyClientExample.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.channels.ByteChannel;

public class ProxyClientExample extends ExampleClient {

public ProxyClientExample( URI serverURI , InetSocketAddress proxy ) {
super( serverURI );
setProxy( proxy );
}

@Override
public ByteChannel createProxyChannel( ByteChannel towrap ) {
/*
* You can create custom proxy handshake here.
* For more infos see: WebSocketClient.DefaultClientProxyChannel and http://tools.ietf.org/html/rfc6455#section-4.1
*/
return super.createProxyChannel( towrap );
}

public class ProxyClientExample {
public static void main( String[] args ) throws URISyntaxException {
ProxyClientExample c = new ProxyClientExample( new URI( "ws://echo.websocket.org" ), new InetSocketAddress( "proxyaddress", 80 ) );// don't forget to change "proxyaddress"
ExampleClient c = new ExampleClient( new URI( "ws://echo.websocket.org" ) );
c.setProxy( new Proxy( Proxy.Type.HTTP, new InetSocketAddress( "proxyaddress", 80 ) ) );
c.connect();
}
}
6 changes: 4 additions & 2 deletions src/main/example/SSLClientExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManagerFactory;

import org.java_websocket.WebSocketImpl;
import org.java_websocket.client.DefaultSSLWebSocketClientFactory;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

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

chatclient.setWebSocketFactory( new DefaultSSLWebSocketClientFactory( sslContext ) );
SSLSocketFactory factory = sslContext.getSocketFactory();// (SSLSocketFactory) SSLSocketFactory.getDefault();

chatclient.setSocket( factory.createSocket() );

chatclient.connectBlocking();

Expand Down
14 changes: 3 additions & 11 deletions src/main/java/org/java_websocket/SocketChannelIOHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.nio.channels.ByteChannel;
import java.nio.channels.spi.AbstractSelectableChannel;

import org.java_websocket.WebSocket.Role;

public class SocketChannelIOHelper {

public static boolean read( final ByteBuffer buf, WebSocketImpl ws, ByteChannel channel ) throws IOException {
Expand Down Expand Up @@ -59,21 +61,11 @@ public static boolean batch( WebSocketImpl ws, ByteChannel sockchannel ) throws
} while ( buffer != null );
}

if( ws.outQueue.isEmpty() && ws.isFlushAndClose() /*&& ( c == null || c.isNeedWrite() )*/) {
if( ws.outQueue.isEmpty() && ws.isFlushAndClose() && ws.getDraft().getRole() == Role.SERVER ) {//
synchronized ( ws ) {
ws.closeConnection();
}
}
return c != null ? !( (WrappedByteChannel) sockchannel ).isNeedWrite() : true;
}

public static void writeBlocking( WebSocketImpl ws, ByteChannel channel ) throws InterruptedException , IOException {
assert ( channel instanceof AbstractSelectableChannel == true ? ( (AbstractSelectableChannel) channel ).isBlocking() : true );
assert ( channel instanceof WrappedByteChannel == true ? ( (WrappedByteChannel) channel ).isBlocking() : true );

ByteBuffer buf = ws.outQueue.take();
while ( buf.hasRemaining() )
channel.write( buf );
}

}
20 changes: 17 additions & 3 deletions src/main/java/org/java_websocket/WebSocketAdapter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.java_websocket;

import java.net.InetSocketAddress;

import org.java_websocket.drafts.Draft;
import org.java_websocket.exceptions.InvalidDataException;
import org.java_websocket.exceptions.InvalidHandshakeException;
import org.java_websocket.framing.Framedata;
import org.java_websocket.framing.Framedata.Opcode;
import org.java_websocket.framing.FramedataImpl1;
Expand Down Expand Up @@ -79,12 +82,23 @@ public void onWebsocketPong( WebSocket conn, Framedata f ) {
* This is specifically implemented for gitime's WebSocket client for Flash:
* http://github.com/gimite/web-socket-js
*
* @return An XML String that comforms to Flash's security policy. You MUST
* @return An XML String that comforts to Flash's security policy. You MUST
* not include the null char at the end, it is appended automatically.
* @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.
*/
@Override
public String getFlashPolicy( WebSocket conn ) {
return "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" + conn.getLocalSocketAddress().getPort() + "\" /></cross-domain-policy>\0";
public String getFlashPolicy( WebSocket conn ) throws InvalidDataException {
InetSocketAddress adr = conn.getLocalSocketAddress();
if(null == adr){
throw new InvalidHandshakeException( "socket not bound" );
}

StringBuffer sb = new StringBuffer( 90 );
sb.append( "<cross-domain-policy><allow-access-from domain=\"*\" to-ports=\"" );
sb.append(adr.getPort());
sb.append( "\" /></cross-domain-policy>\0" );

return sb.toString();
}

}
20 changes: 8 additions & 12 deletions src/main/java/org/java_websocket/WebSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,11 @@ public WebSocketImpl( WebSocketListener listener , List<Draft> drafts , Socket s
public void decode( ByteBuffer socketBuffer ) {
assert ( socketBuffer.hasRemaining() );

if( flushandclosestate ) {
return;
}

if( DEBUG )
System.out.println( "process(" + socketBuffer.remaining() + "): {" + ( socketBuffer.remaining() > 1000 ? "too big to display" : new String( socketBuffer.array(), socketBuffer.position(), socketBuffer.remaining() ) ) + "}" );

if( readystate == READYSTATE.OPEN ) {
decodeFrames( socketBuffer );
if( readystate != READYSTATE.NOT_YET_CONNECTED ) {
decodeFrames( socketBuffer );;
} else {
if( decodeHandshake( socketBuffer ) ) {
assert ( tmpHandshakeBytes.hasRemaining() != socketBuffer.hasRemaining() || !socketBuffer.hasRemaining() ); // the buffers will never have remaining bytes at the same time
Expand Down Expand Up @@ -198,8 +194,12 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
if( draft == null ) {
HandshakeState isflashedgecase = isFlashEdgeCase( socketBuffer );
if( isflashedgecase == HandshakeState.MATCHED ) {
write( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
close( CloseFrame.FLASHPOLICY, "" );
try {
write( ByteBuffer.wrap( Charsetfunctions.utf8Bytes( wsl.getFlashPolicy( this ) ) ) );
close( CloseFrame.FLASHPOLICY, "" );
} catch ( InvalidDataException e ) {
close( CloseFrame.ABNORMAL_CLOSE, "remote peer closed connection before flashpolicy could be transmitted", true );
}
return false;
}
}
Expand Down Expand Up @@ -315,17 +315,13 @@ private boolean decodeHandshake( ByteBuffer socketBufferNew ) {
}

private void decodeFrames( ByteBuffer socketBuffer ) {
if( flushandclosestate )
return;

List<Framedata> frames;
try {
frames = draft.translateFrame( socketBuffer );
for( Framedata f : frames ) {
if( DEBUG )
System.out.println( "matched frame: " + f );
if( flushandclosestate )
return;
Opcode curop = f.getOpcode();
boolean fin = f.isFin();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/java_websocket/WebSocketListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ public interface WebSocketListener {
/**
* Gets the XML string that should be returned if a client requests a Flash
* security policy.
* @throws InvalidDataException thrown when some data that is required to generate the flash-policy like the websocket local port could not be obtained.
*/
public String getFlashPolicy( WebSocket conn );
public String getFlashPolicy( WebSocket conn ) throws InvalidDataException;

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

This file was deleted.

This file was deleted.

Loading