From ab9e85836725414a6badd999a4017fe51d5d7b99 Mon Sep 17 00:00:00 2001 From: Philip Roman Date: Fri, 25 May 2018 13:20:48 +0300 Subject: [PATCH 1/3] Added WebSocketServer.broadcast methods for ByteBuffers --- .../server/WebSocketServer.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/org/java_websocket/server/WebSocketServer.java b/src/main/java/org/java_websocket/server/WebSocketServer.java index 6402e7a06..5b9342e9d 100644 --- a/src/main/java/org/java_websocket/server/WebSocketServer.java +++ b/src/main/java/org/java_websocket/server/WebSocketServer.java @@ -812,6 +812,10 @@ public void broadcast(byte[] data) { broadcast( data, connections ); } + public void broadcast(ByteBuffer data) { + broadcast(data, connections); + } + /** * Send a byte array to a specific collection of websocket connections * @param data the data to send to the endpoints @@ -841,6 +845,29 @@ public void broadcast(byte[] data, Collection clients) { } } + public void broadcast(ByteBuffer data, Collection clients) { + if (data == null || clients == null) { + throw new IllegalArgumentException(); + } + Map> draftFrames = new HashMap>(); + synchronized( clients ) { + for( WebSocket client : clients ) { + if( client != null ) { + Draft draft = client.getDraft(); + if( !draftFrames.containsKey( draft ) ) { + List frames = draft.createFrames( data, false ); + draftFrames.put( draft, frames ); + } + try { + client.sendFrame( draftFrames.get( draft ) ); + } catch ( WebsocketNotConnectedException e ) { + //Ignore this exception in this case + } + } + } + } + } + /** * Send a text to a specific collection of websocket connections * @param text the text to send to the endpoints From 135b3c6916cb92b277fe4599e975318a76096e45 Mon Sep 17 00:00:00 2001 From: Philip Roman Date: Fri, 25 May 2018 13:43:47 +0300 Subject: [PATCH 2/3] Added documentation for new broadcast methods --- .../java/org/java_websocket/server/WebSocketServer.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/org/java_websocket/server/WebSocketServer.java b/src/main/java/org/java_websocket/server/WebSocketServer.java index 5b9342e9d..119a7884f 100644 --- a/src/main/java/org/java_websocket/server/WebSocketServer.java +++ b/src/main/java/org/java_websocket/server/WebSocketServer.java @@ -812,6 +812,10 @@ public void broadcast(byte[] data) { broadcast( data, connections ); } + /** + * Send a ByteBuffer to all connected endpoints + * @param data the data to send to the endpoints + */ public void broadcast(ByteBuffer data) { broadcast(data, connections); } @@ -845,6 +849,11 @@ public void broadcast(byte[] data, Collection clients) { } } + /** + * Send a ByteBuffer to a specific collection of websocket connections + * @param data the data to send to the endpoints + * @param clients a collection of endpoints to whom the text has to be send + */ public void broadcast(ByteBuffer data, Collection clients) { if (data == null || clients == null) { throw new IllegalArgumentException(); From c074a944f40b0a99e1824d84b1ab89661110c6e8 Mon Sep 17 00:00:00 2001 From: Philip Roman Date: Fri, 25 May 2018 13:59:06 +0300 Subject: [PATCH 3/3] Removed duplicated code from broadcast methods --- .../server/WebSocketServer.java | 19 +------------------ 1 file changed, 1 insertion(+), 18 deletions(-) diff --git a/src/main/java/org/java_websocket/server/WebSocketServer.java b/src/main/java/org/java_websocket/server/WebSocketServer.java index 119a7884f..39c944c2b 100644 --- a/src/main/java/org/java_websocket/server/WebSocketServer.java +++ b/src/main/java/org/java_websocket/server/WebSocketServer.java @@ -829,24 +829,7 @@ public void broadcast(byte[] data, Collection clients) { if (data == null || clients == null) { throw new IllegalArgumentException(); } - Map> draftFrames = new HashMap>(); - ByteBuffer byteBufferData = ByteBuffer.wrap( data ); - synchronized( clients ) { - for( WebSocket client : clients ) { - if( client != null ) { - Draft draft = client.getDraft(); - if( !draftFrames.containsKey( draft ) ) { - List frames = draft.createFrames( byteBufferData, false ); - draftFrames.put( draft, frames ); - } - try { - client.sendFrame( draftFrames.get( draft ) ); - } catch ( WebsocketNotConnectedException e ) { - //Ignore this exception in this case - } - } - } - } + broadcast(ByteBuffer.wrap(data), clients); } /**