16
16
17
17
import org .java_websocket .SocketChannelIOHelper ;
18
18
import org .java_websocket .WebSocket ;
19
- import org .java_websocket .WebSocket .READYSTATE ;
20
19
import org .java_websocket .WebSocketAdapter ;
21
20
import org .java_websocket .WebSocketFactory ;
22
21
import org .java_websocket .WebSocketImpl ;
25
24
import org .java_websocket .drafts .Draft_10 ;
26
25
import org .java_websocket .exceptions .InvalidHandshakeException ;
27
26
import org .java_websocket .framing .CloseFrame ;
27
+ import org .java_websocket .framing .Framedata ;
28
+ import org .java_websocket .framing .Framedata .Opcode ;
28
29
import org .java_websocket .handshake .HandshakeImpl1Client ;
29
30
import org .java_websocket .handshake .Handshakedata ;
30
31
import org .java_websocket .handshake .ServerHandshake ;
39
40
*
40
41
* @author Nathan Rajlich
41
42
*/
42
- public abstract class WebSocketClient extends WebSocketAdapter implements Runnable {
43
+ public abstract class WebSocketClient extends WebSocketAdapter implements Runnable , WebSocket {
43
44
44
45
/**
45
46
* The URI this channel is supposed to connect to.
@@ -104,14 +105,13 @@ public WebSocketClient( URI serverUri , Draft draft , Map<String,String> headers
104
105
channel = null ;
105
106
onWebsocketError ( null , e );
106
107
}
107
- if (channel == null ) {
108
+ if ( channel == null ) {
108
109
conn = (WebSocketImpl ) wsfactory .createWebSocket ( this , draft , null );
109
110
conn .close ( CloseFrame .NEVER_CONNECTED , "Failed to create or configure SocketChannel." );
110
- }
111
- else {
111
+ } else {
112
112
conn = (WebSocketImpl ) wsfactory .createWebSocket ( this , draft , channel .socket () );
113
113
}
114
-
114
+
115
115
}
116
116
117
117
/**
@@ -171,14 +171,18 @@ public void send( String text ) throws NotYetConnectedException {
171
171
conn .send ( text );
172
172
}
173
173
174
+ public void sendFragment ( Framedata f ) {
175
+ conn .sendFrame ( f );
176
+ }
177
+
174
178
/**
175
179
* Sends <var>data</var> to the connected WebSocket server.
176
180
*
177
181
* @param data
178
182
* The Byte-Array of data to send to the WebSocket server.
179
183
*/
180
184
public void send ( byte [] data ) throws NotYetConnectedException {
181
- conn .send ( data );
185
+ conn .send ( data );
182
186
}
183
187
184
188
// Runnable IMPLEMENTATION /////////////////////////////////////////////////
@@ -198,7 +202,7 @@ private final void interruptableRun() {
198
202
199
203
try {
200
204
String host ;
201
- int port ;
205
+ int port ;
202
206
203
207
if ( proxyAddress != null ) {
204
208
host = proxyAddress .getHostName ();
@@ -317,6 +321,11 @@ public final void onWebsocketMessage( WebSocket conn, ByteBuffer blob ) {
317
321
onMessage ( blob );
318
322
}
319
323
324
+ @ Override
325
+ public void onWebsocketMessageFragment ( WebSocket conn , Framedata frame ) {
326
+ onFragment ( frame );
327
+ }
328
+
320
329
/**
321
330
* Calls subclass' implementation of <var>onOpen</var>.
322
331
*
@@ -405,7 +414,9 @@ public InetSocketAddress getRemoteSocketAddress( WebSocket conn ) {
405
414
public abstract void onClose ( int code , String reason , boolean remote );
406
415
public abstract void onError ( Exception ex );
407
416
public void onMessage ( ByteBuffer bytes ) {
408
- };
417
+ }
418
+ public void onFragment ( Framedata frame ) {
419
+ }
409
420
410
421
public class DefaultClientProxyChannel extends AbstractClientProxyChannel {
411
422
public DefaultClientProxyChannel ( ByteChannel towrap ) {
@@ -446,15 +457,84 @@ public void run() {
446
457
}
447
458
}
448
459
}
449
-
460
+
450
461
public ByteChannel createProxyChannel ( ByteChannel towrap ) {
451
- if ( proxyAddress != null ){
462
+ if ( proxyAddress != null ) {
452
463
return new DefaultClientProxyChannel ( towrap );
453
464
}
454
- return towrap ;//no proxy in use
465
+ return towrap ;// no proxy in use
455
466
}
456
467
457
468
public void setProxy ( InetSocketAddress proxyaddress ) {
458
469
proxyAddress = proxyaddress ;
459
470
}
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
+ }
460
540
}
0 commit comments