Skip to content

Commit d4d99ed

Browse files
committed
Introduced method sendFragmentedFrame() to send fragmented frames more
convenient.
1 parent 4e27fa8 commit d4d99ed

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/main/java/org/java_websocket/WebSocket.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.java_websocket.drafts.Draft;
88
import org.java_websocket.framing.Framedata;
9+
import org.java_websocket.framing.Framedata.Opcode;
910

1011
public interface WebSocket {
1112
public enum Role {
@@ -62,6 +63,21 @@ public enum READYSTATE {
6263

6364
public abstract void sendFrame( Framedata framedata );
6465

66+
/**
67+
* Allows to send continuous/fragmented frames conveniently. <br>
68+
* For more into on this frame type see http://tools.ietf.org/html/rfc6455#section-5.4<br>
69+
*
70+
* If the first frame you send is also the last then it is not a fragmented frame and will received via onMessage instead of onFragmented even though it was send by this method.
71+
*
72+
* @param op
73+
* This is only important for the first frame in the sequence. Opcode.TEXT, Opcode.BINARY are allowed.
74+
* @param buffer
75+
* The buffer which contains the payload. It may have no bytes remaining.
76+
* @param fin
77+
* true means the current frame is the last in the sequence.
78+
**/
79+
public abstract void sendFragmentedFrame( Opcode op, ByteBuffer buffer, boolean fin );
80+
6581
public abstract boolean hasBufferedData();
6682

6783
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,11 @@ private void send( Collection<Framedata> frames ) {
577577
}
578578
}
579579

580+
@Override
581+
public void sendFragmentedFrame( Opcode op, ByteBuffer buffer, boolean fin ) {
582+
send( draft.continuousFrame( op, buffer, fin ) );
583+
}
584+
580585
@Override
581586
public void sendFrame( Framedata framedata ) {
582587
if( DEBUG )

src/main/java/org/java_websocket/drafts/Draft.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import org.java_websocket.exceptions.InvalidHandshakeException;
1313
import org.java_websocket.exceptions.LimitExedeedException;
1414
import org.java_websocket.framing.CloseFrame;
15+
import org.java_websocket.framing.FrameBuilder;
1516
import org.java_websocket.framing.Framedata;
17+
import org.java_websocket.framing.Framedata.Opcode;
18+
import org.java_websocket.framing.FramedataImpl1;
1619
import org.java_websocket.handshake.ClientHandshake;
1720
import org.java_websocket.handshake.ClientHandshakeBuilder;
1821
import org.java_websocket.handshake.HandshakeBuilder;
@@ -46,6 +49,8 @@ public enum CloseHandshakeType {
4649
/** In some cases the handshake will be parsed different depending on whether */
4750
protected Role role = null;
4851

52+
protected Opcode continuousFrameType = null;
53+
4954
public static ByteBuffer readLine( ByteBuffer buf ) {
5055
ByteBuffer sbuf = ByteBuffer.allocate( buf.remaining() );
5156
byte prev = '0';
@@ -123,6 +128,34 @@ protected boolean basicAccept( Handshakedata handshakedata ) {
123128

124129
public abstract List<Framedata> createFrames( String text, boolean mask );
125130

131+
public List<Framedata> continuousFrame( Opcode op, ByteBuffer buffer, boolean fin ) {
132+
if( op != Opcode.BINARY && op != Opcode.TEXT && op != Opcode.TEXT ) {
133+
throw new IllegalArgumentException( "Only Opcode.BINARY or Opcode.TEXT are allowed" );
134+
}
135+
136+
if( continuousFrameType != null ) {
137+
continuousFrameType = Opcode.CONTINUOUS;
138+
} else if( fin ) {
139+
throw new IllegalArgumentException( "There is no continious frame to continue" );
140+
} else {
141+
continuousFrameType = op;
142+
}
143+
144+
FrameBuilder bui = new FramedataImpl1( continuousFrameType );
145+
try {
146+
bui.setPayload( buffer );
147+
} catch ( InvalidDataException e ) {
148+
throw new RuntimeException( e ); // can only happen when one builds close frames(Opcode.Close)
149+
}
150+
bui.setFin( fin );
151+
if( fin ) {
152+
continuousFrameType = null;
153+
} else {
154+
continuousFrameType = op;
155+
}
156+
return Collections.singletonList( (Framedata) bui );
157+
}
158+
126159
public abstract void reset();
127160

128161
public List<ByteBuffer> createHandshake( Handshakedata handshakedata, Role ownrole ) {

0 commit comments

Comments
 (0)