Skip to content

Commit fe11114

Browse files
committed
Added example on how send fragmented frames.
1 parent e1676f5 commit fe11114

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.net.URI;
5+
import java.net.URISyntaxException;
6+
import java.nio.ByteBuffer;
7+
8+
import org.java_websocket.WebSocket;
9+
import org.java_websocket.client.WebSocketClient;
10+
import org.java_websocket.drafts.Draft_17;
11+
import org.java_websocket.framing.Framedata.Opcode;
12+
13+
/**
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.
17+
*
18+
* @see WebSocket#sendFragmentedFrame(Opcode, ByteBuffer, boolean)
19+
**/
20+
public class FragmentedFramesExample {
21+
public static void main( String[] args ) throws URISyntaxException , IOException , InterruptedException {
22+
// WebSocketImpl.DEBUG = true; // will give extra output
23+
24+
WebSocketClient websocket = new ExampleClient( new URI( "ws://localhost:8887" ), new Draft_17() ); // Draft_17 is implementation of rfc6455
25+
if( !websocket.connectBlocking() ) {
26+
System.err.println( "Could not connect to the server." );
27+
return;
28+
}
29+
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+
32+
BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );
33+
while ( websocket.isOpen() ) {
34+
System.out.println( "Please type in a loooooong line(which will be send in multible parts):" );
35+
String longline = stdin.readLine();
36+
ByteBuffer longelinebuffer = ByteBuffer.wrap( longline.getBytes() );
37+
longelinebuffer.rewind();
38+
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." );
40+
for( int position = 2 ; ; position += 2 ) {
41+
42+
String sendInOnePiece = stdin.readLine();
43+
if( !sendInOnePiece.isEmpty() ) {
44+
websocket.send( sendInOnePiece );
45+
}
46+
47+
if( position < longelinebuffer.capacity() ) {
48+
longelinebuffer.limit( position );
49+
websocket.sendFragmentedFrame( Opcode.TEXT, longelinebuffer, false );// when sending binary data use Opcode.BINARY
50+
} else {
51+
longelinebuffer.limit( longelinebuffer.capacity() );
52+
websocket.sendFragmentedFrame( Opcode.TEXT, longelinebuffer, true );
53+
break;
54+
}
55+
56+
}
57+
System.out.println( "You can not type in the next long message or press Ctr-C to exit." );
58+
}
59+
System.out.println( "FragmentedFramesExample terminated" );
60+
}
61+
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ public List<Framedata> continuousFrame( Opcode op, ByteBuffer buffer, boolean fi
135135

136136
if( continuousFrameType != null ) {
137137
continuousFrameType = Opcode.CONTINUOUS;
138-
} else if( fin ) {
139-
throw new IllegalArgumentException( "There is no continious frame to continue" );
140138
} else {
141139
continuousFrameType = op;
142140
}

0 commit comments

Comments
 (0)