Skip to content

Commit f34bc4d

Browse files
committed
corrected logical errors in FragmentedFramesExample
1 parent effeb32 commit f34bc4d

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/main/example/ExampleClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void onMessage( String message ) {
3131

3232
@Override
3333
public void onFragment( Framedata fragment ) {
34-
System.out.println( "received fragment: " + fragment );
34+
System.out.println( "received fragment: " + new String( fragment.getPayloadData().array() ) );
3535
}
3636

3737
@Override

src/main/example/FragmentedFramesExample.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
import org.java_websocket.framing.Framedata.Opcode;
1212

1313
/**
14-
* This example show how to send fragmented frames.<br>
15-
* It also shows that one can mix fragmented and normal frames at will.<br>
16-
* Of course one has to finish with a fragmented frame sequence before continuing with the next.
14+
* This example shows how to send fragmented frames.<br>
15+
* For information on when to used fragmented frames see http://tools.ietf.org/html/rfc6455#section-5.4<br>
16+
* Fragmented and normal messages can not be mixed.
17+
* One is however allowed to mix them with control messages like ping/pong.
1718
*
1819
* @see WebSocket#sendFragmentedFrame(Opcode, ByteBuffer, boolean)
1920
**/
@@ -27,29 +28,24 @@ public static void main( String[] args ) throws URISyntaxException , IOException
2728
return;
2829
}
2930

30-
System.out.println( "This example shows how to send fragmented(continuous) messages.\n It also shows that fragments can be intercepted by normal messages." );
31+
System.out.println( "This example shows how to send fragmented(continuous) messages." );
3132

3233
BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
3334
while ( websocket.isOpen() ) {
34-
System.out.println( "Please type in a loooooong line(which will be send in multible parts):" );
35+
System.out.println( "Please type in a loooooong line(which then will be send in 2 byte fragments):" );
3536
String longline = stdin.readLine();
3637
ByteBuffer longelinebuffer = ByteBuffer.wrap( longline.getBytes() );
3738
longelinebuffer.rewind();
3839

39-
System.out.println( "The long message you just typed in will be fragmented in messages of 2bytes payload each.\nPress enter so send the next fragemnt or make some other input to send text messages inbetween." );
4040
for( int position = 2 ; ; position += 2 ) {
41-
42-
String sendInOnePiece = stdin.readLine();
43-
if( !sendInOnePiece.isEmpty() ) {
44-
websocket.send( sendInOnePiece );
45-
}
46-
4741
if( position < longelinebuffer.capacity() ) {
4842
longelinebuffer.limit( position );
49-
websocket.sendFragmentedFrame( Opcode.TEXT, longelinebuffer, false );// when sending binary data use Opcode.BINARY
43+
websocket.sendFragmentedFrame( Opcode.TEXT, longelinebuffer, false );// when sending binary data one should use Opcode.BINARY
44+
assert ( longelinebuffer.remaining() == 0 );
45+
// after calling sendFragmentedFrame one may reuse the buffer given to the method immediately
5046
} else {
5147
longelinebuffer.limit( longelinebuffer.capacity() );
52-
websocket.sendFragmentedFrame( Opcode.TEXT, longelinebuffer, true );
48+
websocket.sendFragmentedFrame( Opcode.TEXT, longelinebuffer, true );// sending the last frame
5349
break;
5450
}
5551

0 commit comments

Comments
 (0)