Skip to content

Commit 7ea8526

Browse files
committed
Made WebSocketClient implement the WebSocket interface.
Reformatted code.
1 parent d4d99ed commit 7ea8526

File tree

1 file changed

+92
-12
lines changed

1 file changed

+92
-12
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import org.java_websocket.SocketChannelIOHelper;
1818
import org.java_websocket.WebSocket;
19-
import org.java_websocket.WebSocket.READYSTATE;
2019
import org.java_websocket.WebSocketAdapter;
2120
import org.java_websocket.WebSocketFactory;
2221
import org.java_websocket.WebSocketImpl;
@@ -25,6 +24,8 @@
2524
import org.java_websocket.drafts.Draft_10;
2625
import org.java_websocket.exceptions.InvalidHandshakeException;
2726
import org.java_websocket.framing.CloseFrame;
27+
import org.java_websocket.framing.Framedata;
28+
import org.java_websocket.framing.Framedata.Opcode;
2829
import org.java_websocket.handshake.HandshakeImpl1Client;
2930
import org.java_websocket.handshake.Handshakedata;
3031
import org.java_websocket.handshake.ServerHandshake;
@@ -39,7 +40,7 @@
3940
*
4041
* @author Nathan Rajlich
4142
*/
42-
public abstract class WebSocketClient extends WebSocketAdapter implements Runnable {
43+
public abstract class WebSocketClient extends WebSocketAdapter implements Runnable, WebSocket {
4344

4445
/**
4546
* The URI this channel is supposed to connect to.
@@ -104,14 +105,13 @@ public WebSocketClient( URI serverUri , Draft draft , Map<String,String> headers
104105
channel = null;
105106
onWebsocketError( null, e );
106107
}
107-
if(channel == null){
108+
if( channel == null ) {
108109
conn = (WebSocketImpl) wsfactory.createWebSocket( this, draft, null );
109110
conn.close( CloseFrame.NEVER_CONNECTED, "Failed to create or configure SocketChannel." );
110-
}
111-
else{
111+
} else {
112112
conn = (WebSocketImpl) wsfactory.createWebSocket( this, draft, channel.socket() );
113113
}
114-
114+
115115
}
116116

117117
/**
@@ -171,14 +171,18 @@ public void send( String text ) throws NotYetConnectedException {
171171
conn.send( text );
172172
}
173173

174+
public void sendFragment( Framedata f ) {
175+
conn.sendFrame( f );
176+
}
177+
174178
/**
175179
* Sends <var>data</var> to the connected WebSocket server.
176180
*
177181
* @param data
178182
* The Byte-Array of data to send to the WebSocket server.
179183
*/
180184
public void send( byte[] data ) throws NotYetConnectedException {
181-
conn.send( data );
185+
conn.send( data );
182186
}
183187

184188
// Runnable IMPLEMENTATION /////////////////////////////////////////////////
@@ -198,7 +202,7 @@ private final void interruptableRun() {
198202

199203
try {
200204
String host;
201-
int port ;
205+
int port;
202206

203207
if( proxyAddress != null ) {
204208
host = proxyAddress.getHostName();
@@ -317,6 +321,11 @@ public final void onWebsocketMessage( WebSocket conn, ByteBuffer blob ) {
317321
onMessage( blob );
318322
}
319323

324+
@Override
325+
public void onWebsocketMessageFragment( WebSocket conn, Framedata frame ) {
326+
onFragment( frame );
327+
}
328+
320329
/**
321330
* Calls subclass' implementation of <var>onOpen</var>.
322331
*
@@ -405,7 +414,9 @@ public InetSocketAddress getRemoteSocketAddress( WebSocket conn ) {
405414
public abstract void onClose( int code, String reason, boolean remote );
406415
public abstract void onError( Exception ex );
407416
public void onMessage( ByteBuffer bytes ) {
408-
};
417+
}
418+
public void onFragment( Framedata frame ) {
419+
}
409420

410421
public class DefaultClientProxyChannel extends AbstractClientProxyChannel {
411422
public DefaultClientProxyChannel( ByteChannel towrap ) {
@@ -446,15 +457,84 @@ public void run() {
446457
}
447458
}
448459
}
449-
460+
450461
public ByteChannel createProxyChannel( ByteChannel towrap ) {
451-
if( proxyAddress != null ){
462+
if( proxyAddress != null ) {
452463
return new DefaultClientProxyChannel( towrap );
453464
}
454-
return towrap;//no proxy in use
465+
return towrap;// no proxy in use
455466
}
456467

457468
public void setProxy( InetSocketAddress proxyaddress ) {
458469
proxyAddress = proxyaddress;
459470
}
471+
472+
@Override
473+
public void sendFragmentedFrame( Opcode op, ByteBuffer buffer, boolean fin ) {
474+
conn.sendFragmentedFrame( op, buffer, fin );
475+
}
476+
477+
@Override
478+
public boolean isOpen() {
479+
return conn.isOpen();
480+
}
481+
482+
@Override
483+
public boolean isFlushAndClose() {
484+
return conn.isFlushAndClose();
485+
}
486+
487+
@Override
488+
public boolean isClosed() {
489+
return conn.isClosed();
490+
}
491+
492+
@Override
493+
public boolean isClosing() {
494+
return conn.isClosing();
495+
}
496+
497+
@Override
498+
public boolean isConnecting() {
499+
return conn.isConnecting();
500+
}
501+
502+
@Override
503+
public boolean hasBufferedData() {
504+
return conn.hasBufferedData();
505+
}
506+
507+
@Override
508+
public void close( int code ) {
509+
conn.close();
510+
}
511+
512+
@Override
513+
public void close( int code, String message ) {
514+
conn.close( code, message );
515+
}
516+
517+
@Override
518+
public void closeConnection( int code, String message ) {
519+
conn.closeConnection( code, message );
520+
}
521+
522+
@Override
523+
public void send( ByteBuffer bytes ) throws IllegalArgumentException , NotYetConnectedException {
524+
conn.send( bytes );
525+
}
526+
527+
@Override
528+
public void sendFrame( Framedata framedata ) {
529+
conn.sendFrame( framedata );
530+
}
531+
532+
@Override
533+
public InetSocketAddress getLocalSocketAddress() {
534+
return conn.getLocalSocketAddress();
535+
}
536+
@Override
537+
public InetSocketAddress getRemoteSocketAddress() {
538+
return conn.getRemoteSocketAddress();
539+
}
460540
}

0 commit comments

Comments
 (0)