Skip to content

Commit 820bb33

Browse files
committed
server sendTo and sendToAll methods now will use fragments if needed
This change allows supporting sending large messages by sending keepalive messages in between fragments. Previously, the timeout mechanism in Java-WebSocket would close the connection automatically because no ping-pongs would be exchanged while the client was receiving a large message.
1 parent 8282a50 commit 820bb33

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

jar/matlab-websocket-1.4.jar

-117 KB
Binary file not shown.

jar/matlab-websocket-1.5.jar

177 KB
Binary file not shown.

src/matlab-websocket/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>io.github.jebej.matlabwebsocket</groupId>
66
<artifactId>matlab-websocket</artifactId>
77
<packaging>jar</packaging>
8-
<version>1.4</version>
8+
<version>1.5</version>
99

1010
<name>matlab-websocket</name>
1111
<url>https://github.com/jebej/MatlabWebSocket</url>

src/matlab-websocket/src/main/java/io/github/jebej/matlabwebsocket/MatlabWebSocketServer.java

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.java_websocket.WebSocket;
1010
import org.java_websocket.handshake.ClientHandshake;
1111
import org.java_websocket.server.WebSocketServer;
12+
import org.java_websocket.framing.PongFrame;
13+
import org.java_websocket.enums.Opcode;
1214

1315
public class MatlabWebSocketServer extends WebSocketServer {
1416
// The constructor creates a new WebSocketServer with the wildcard IP,
@@ -76,9 +78,9 @@ public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
7678
public WebSocket getConnection( int hashCode ) {
7779
Collection<WebSocket> conns = getConnections();
7880
synchronized ( conns ) {
79-
for( WebSocket c : conns ) {
80-
if (c.hashCode() == hashCode) {
81-
return c;
81+
for ( WebSocket conn : conns ) {
82+
if (conn.hashCode() == hashCode) {
83+
return conn;
8284
}
8385
}
8486
}
@@ -92,7 +94,8 @@ public void sendTo( int hashCode, String message ) {
9294

9395
// Send binary message to a connection identified by a hashcode
9496
public void sendTo( int hashCode, ByteBuffer blob ) {
95-
getConnection( hashCode ).send( blob );
97+
WebSocket conn = getConnection( hashCode );
98+
sendSplit( conn, blob );
9699
}
97100

98101
// Send binary message to a connection identified by a hashcode
@@ -104,8 +107,8 @@ public void sendTo( int hashCode, byte[] bytes ) {
104107
public void sendToAll( String message ) {
105108
Collection<WebSocket> conns = getConnections();
106109
synchronized ( conns ) {
107-
for( WebSocket c : conns ) {
108-
c.send( message );
110+
for( WebSocket conn : conns ) {
111+
conn.send( message );
109112
}
110113
}
111114
}
@@ -114,8 +117,8 @@ public void sendToAll( String message ) {
114117
public void sendToAll( ByteBuffer blob ) {
115118
Collection<WebSocket> conns = getConnections();
116119
synchronized ( conns ) {
117-
for( WebSocket c : conns ) {
118-
c.send( blob );
120+
for( WebSocket conn : conns ) {
121+
sendSplit( conn, blob );
119122
}
120123
}
121124
}
@@ -124,6 +127,28 @@ public void sendToAll( ByteBuffer blob ) {
124127
public void sendToAll( byte[] bytes ) {
125128
sendToAll( ByteBuffer.wrap( bytes ) );
126129
}
130+
131+
// Method to send large messages in fragments if needed
132+
public void sendSplit( WebSocket conn, ByteBuffer blob ) {
133+
int FRAG_SIZE = 5*1024*1024; // 5MB
134+
int blobSize = blob.capacity();
135+
// Only send as fragments if message is larger than FRAG_SIZE
136+
if ( blobSize <= FRAG_SIZE ) {
137+
conn.send( blob );
138+
} else {
139+
int numFrags = (blobSize + FRAG_SIZE - 1)/FRAG_SIZE;
140+
blob.rewind();
141+
for ( int i = 0; i<numFrags; i++ ) {
142+
blob.position( i*FRAG_SIZE );
143+
blob.limit( Math.min( (i+1)*FRAG_SIZE, blobSize ) );
144+
conn.sendFragmentedFrame( Opcode.BINARY, blob, (i+1)==numFrags );
145+
// Send a ping AND an unpromted pong to keep connection alive
146+
conn.sendPing();
147+
conn.sendFrame( new PongFrame() );
148+
}
149+
assert( blob.position() == blobSize );
150+
}
151+
}
127152

128153
// Close connection identified by a hashcode
129154
public void close( int hashCode ) {

0 commit comments

Comments
 (0)