diff --git a/jar/matlab-websocket-1.4.jar b/jar/matlab-websocket-1.4.jar deleted file mode 100644 index 6e3947a..0000000 Binary files a/jar/matlab-websocket-1.4.jar and /dev/null differ diff --git a/jar/matlab-websocket-1.5.jar b/jar/matlab-websocket-1.5.jar new file mode 100644 index 0000000..127bb75 Binary files /dev/null and b/jar/matlab-websocket-1.5.jar differ diff --git a/src/matlab-websocket/pom.xml b/src/matlab-websocket/pom.xml index 26223cc..41919e3 100644 --- a/src/matlab-websocket/pom.xml +++ b/src/matlab-websocket/pom.xml @@ -5,7 +5,7 @@ io.github.jebej.matlabwebsocket matlab-websocket jar - 1.4 + 1.5 matlab-websocket https://github.com/jebej/MatlabWebSocket diff --git a/src/matlab-websocket/src/main/java/io/github/jebej/matlabwebsocket/MatlabWebSocketServer.java b/src/matlab-websocket/src/main/java/io/github/jebej/matlabwebsocket/MatlabWebSocketServer.java index 8104ddb..9ab2281 100644 --- a/src/matlab-websocket/src/main/java/io/github/jebej/matlabwebsocket/MatlabWebSocketServer.java +++ b/src/matlab-websocket/src/main/java/io/github/jebej/matlabwebsocket/MatlabWebSocketServer.java @@ -9,6 +9,8 @@ import org.java_websocket.WebSocket; import org.java_websocket.handshake.ClientHandshake; import org.java_websocket.server.WebSocketServer; +import org.java_websocket.framing.PongFrame; +import org.java_websocket.enums.Opcode; public class MatlabWebSocketServer extends WebSocketServer { // 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 ) { public WebSocket getConnection( int hashCode ) { Collection conns = getConnections(); synchronized ( conns ) { - for( WebSocket c : conns ) { - if (c.hashCode() == hashCode) { - return c; + for ( WebSocket conn : conns ) { + if (conn.hashCode() == hashCode) { + return conn; } } } @@ -92,7 +94,8 @@ public void sendTo( int hashCode, String message ) { // Send binary message to a connection identified by a hashcode public void sendTo( int hashCode, ByteBuffer blob ) { - getConnection( hashCode ).send( blob ); + WebSocket conn = getConnection( hashCode ); + sendSplit( conn, blob ); } // Send binary message to a connection identified by a hashcode @@ -104,8 +107,8 @@ public void sendTo( int hashCode, byte[] bytes ) { public void sendToAll( String message ) { Collection conns = getConnections(); synchronized ( conns ) { - for( WebSocket c : conns ) { - c.send( message ); + for( WebSocket conn : conns ) { + conn.send( message ); } } } @@ -114,8 +117,8 @@ public void sendToAll( String message ) { public void sendToAll( ByteBuffer blob ) { Collection conns = getConnections(); synchronized ( conns ) { - for( WebSocket c : conns ) { - c.send( blob ); + for( WebSocket conn : conns ) { + sendSplit( conn, blob ); } } } @@ -124,6 +127,28 @@ public void sendToAll( ByteBuffer blob ) { public void sendToAll( byte[] bytes ) { sendToAll( ByteBuffer.wrap( bytes ) ); } + + // Method to send large messages in fragments if needed + public void sendSplit( WebSocket conn, ByteBuffer blob ) { + int FRAG_SIZE = 5*1024*1024; // 5MB + int blobSize = blob.capacity(); + // Only send as fragments if message is larger than FRAG_SIZE + if ( blobSize <= FRAG_SIZE ) { + conn.send( blob ); + } else { + int numFrags = (blobSize + FRAG_SIZE - 1)/FRAG_SIZE; + blob.rewind(); + for ( int i = 0; i