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+
importjava.io.BufferedReader;
2+
importjava.io.IOException;
3+
importjava.io.InputStreamReader;
4+
importjava.net.URI;
5+
importjava.net.URISyntaxException;
6+
importjava.nio.ByteBuffer;
7+
8+
importorg.java_websocket.WebSocket;
9+
importorg.java_websocket.client.WebSocketClient;
10+
importorg.java_websocket.drafts.Draft_17;
11+
importorg.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+
publicclassFragmentedFramesExample{
21+
publicstaticvoidmain( String[] args ) throwsURISyntaxException , IOException , InterruptedException{
22+
// WebSocketImpl.DEBUG = true; // will give extra output
23+
24+
WebSocketClientwebsocket = newExampleClient( newURI( "ws://localhost:8887" ), newDraft_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+
BufferedReaderstdin = newBufferedReader( newInputStreamReader( System.in ) );
33+
while ( websocket.isOpen() ){
34+
System.out.println( "Please type in a loooooong line(which will be send in multible parts):" );
35+
Stringlongline = stdin.readLine();
36+
ByteBufferlongelinebuffer = 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( intposition = 2 ; position += 2 ){
41+
42+
StringsendInOnePiece = 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-
} elseif( fin ){
139-
thrownewIllegalArgumentException( "There is no continious frame to continue" );
140138
} else{
141139
continuousFrameType = op;
142140
}

0 commit comments

Comments
(0)