|
38 | 38 | import java.nio.channels.Selector; |
39 | 39 | import java.nio.channels.ServerSocketChannel; |
40 | 40 | import java.nio.channels.SocketChannel; |
41 | | -import java.util.ArrayList; |
42 | | -import java.util.Collection; |
43 | | -import java.util.Collections; |
44 | | -import java.util.HashSet; |
45 | | -import java.util.Iterator; |
46 | | -import java.util.LinkedList; |
47 | | -import java.util.List; |
48 | | -import java.util.Set; |
| 41 | +import java.util.*; |
49 | 42 | import java.util.concurrent.BlockingQueue; |
50 | 43 | import java.util.concurrent.CopyOnWriteArraySet; |
51 | 44 | import java.util.concurrent.LinkedBlockingQueue; |
|
55 | 48 | import org.java_websocket.*; |
56 | 49 | import org.java_websocket.drafts.Draft; |
57 | 50 | import org.java_websocket.exceptions.InvalidDataException; |
| 51 | +import org.java_websocket.exceptions.WebsocketNotConnectedException; |
58 | 52 | import org.java_websocket.framing.CloseFrame; |
59 | 53 | import org.java_websocket.framing.Framedata; |
60 | 54 | import org.java_websocket.handshake.ClientHandshake; |
@@ -777,6 +771,69 @@ public void onMessage( WebSocket conn, ByteBuffer message ) { |
777 | 771 | public void onFragment( WebSocket conn, Framedata fragment ) { |
778 | 772 | } |
779 | 773 |
|
| 774 | + /** |
| 775 | + * Send a text to all connected endpoints |
| 776 | + * @param text the text to send to the endpoints |
| 777 | + */ |
| 778 | + public void broadcast(String text) { |
| 779 | + broadcast( text, connections ); |
| 780 | + } |
| 781 | + |
| 782 | + /** |
| 783 | + * Send a byte array to all connected endpoints |
| 784 | + * @param data the data to send to the endpoints |
| 785 | + */ |
| 786 | + public void broadcast(byte[] data) { |
| 787 | + broadcast( data, connections ); |
| 788 | + } |
| 789 | + |
| 790 | + /** |
| 791 | + * Send a byte array to a specific collection of websocket connections |
| 792 | + * @param data the data to send to the endpoints |
| 793 | + * @param clients a collection of endpoints to whom the text has to be send |
| 794 | + */ |
| 795 | + public void broadcast(byte[] data, Collection<WebSocket> clients) { |
| 796 | + Map<Draft, List<Framedata>> draftFrames = new HashMap<Draft, List<Framedata>>(); |
| 797 | + ByteBuffer byteBufferData = ByteBuffer.wrap( data ); |
| 798 | + synchronized( clients ) { |
| 799 | + for( WebSocket client : clients ) { |
| 800 | + Draft draft = client.getDraft(); |
| 801 | + if( !draftFrames.containsKey( draft ) ) { |
| 802 | + List<Framedata> frames = draft.createFrames( byteBufferData, false ); |
| 803 | + draftFrames.put( draft, frames ); |
| 804 | + } |
| 805 | + try { |
| 806 | + client.sendFrame( draftFrames.get( draft ) ); |
| 807 | + } catch ( WebsocketNotConnectedException e) { |
| 808 | + //Ignore this exception in this case |
| 809 | + } |
| 810 | + } |
| 811 | + } |
| 812 | + } |
| 813 | + |
| 814 | + /** |
| 815 | + * Send a text to a specific collection of websocket connections |
| 816 | + * @param text the text to send to the endpoints |
| 817 | + * @param clients a collection of endpoints to whom the text has to be send |
| 818 | + */ |
| 819 | + public void broadcast(String text, Collection<WebSocket> clients) { |
| 820 | + Map<Draft, List<Framedata>> draftFrames = new HashMap<Draft, List<Framedata>>(); |
| 821 | + synchronized( clients ) { |
| 822 | + for( WebSocket client : clients ) { |
| 823 | + Draft draft = client.getDraft(); |
| 824 | + if( !draftFrames.containsKey( draft ) ) { |
| 825 | + List<Framedata> frames = draft.createFrames( text, false ); |
| 826 | + draftFrames.put( draft, frames ); |
| 827 | + } |
| 828 | + try { |
| 829 | + client.sendFrame( draftFrames.get( draft ) ); |
| 830 | + } catch ( WebsocketNotConnectedException e) { |
| 831 | + //Ignore this exception in this case |
| 832 | + } |
| 833 | + } |
| 834 | + } |
| 835 | + } |
| 836 | + |
780 | 837 | /** |
781 | 838 | * This class is used to process incoming data |
782 | 839 | */ |
|
0 commit comments