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
importorg.java_websocket.drafts.Draft;
88
importorg.java_websocket.framing.Framedata;
9+
importorg.java_websocket.framing.Framedata.Opcode;
910

1011
publicinterfaceWebSocket{
1112
publicenumRole{
@@ -62,6 +63,21 @@ public enum READYSTATE{
6263

6364
publicabstractvoidsendFrame( Framedataframedata );
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+
publicabstractvoidsendFragmentedFrame( Opcodeop, ByteBufferbuffer, booleanfin );
80+
6581
publicabstractbooleanhasBufferedData();
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+
publicvoidsendFragmentedFrame( Opcodeop, ByteBufferbuffer, booleanfin ){
582+
send( draft.continuousFrame( op, buffer, fin ) );
583+
}
584+
580585
@Override
581586
publicvoidsendFrame( Framedataframedata ){
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
importorg.java_websocket.exceptions.InvalidHandshakeException;
1313
importorg.java_websocket.exceptions.LimitExedeedException;
1414
importorg.java_websocket.framing.CloseFrame;
15+
importorg.java_websocket.framing.FrameBuilder;
1516
importorg.java_websocket.framing.Framedata;
17+
importorg.java_websocket.framing.Framedata.Opcode;
18+
importorg.java_websocket.framing.FramedataImpl1;
1619
importorg.java_websocket.handshake.ClientHandshake;
1720
importorg.java_websocket.handshake.ClientHandshakeBuilder;
1821
importorg.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
protectedRolerole = null;
4851

52+
protectedOpcodecontinuousFrameType = null;
53+
4954
publicstaticByteBufferreadLine( ByteBufferbuf ){
5055
ByteBuffersbuf = ByteBuffer.allocate( buf.remaining() );
5156
byteprev = '0';
@@ -123,6 +128,34 @@ protected boolean basicAccept( Handshakedata handshakedata ){
123128

124129
publicabstractList<Framedata> createFrames( Stringtext, booleanmask );
125130

131+
publicList<Framedata> continuousFrame( Opcodeop, ByteBufferbuffer, booleanfin ){
132+
if( op != Opcode.BINARY && op != Opcode.TEXT && op != Opcode.TEXT ){
133+
thrownewIllegalArgumentException( "Only Opcode.BINARY or Opcode.TEXT are allowed" );
134+
}
135+
136+
if( continuousFrameType != null ){
137+
continuousFrameType = Opcode.CONTINUOUS;
138+
} elseif( fin ){
139+
thrownewIllegalArgumentException( "There is no continious frame to continue" );
140+
} else{
141+
continuousFrameType = op;
142+
}
143+
144+
FrameBuilderbui = newFramedataImpl1( continuousFrameType );
145+
try{
146+
bui.setPayload( buffer );
147+
} catch ( InvalidDataExceptione ){
148+
thrownewRuntimeException( 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+
returnCollections.singletonList( (Framedata) bui );
157+
}
158+
126159
publicabstractvoidreset();
127160

128161
publicList<ByteBuffer> createHandshake( Handshakedatahandshakedata, Roleownrole ){

0 commit comments

Comments
(0)