Skip to content

Commit 40ce57f

Browse files
committed
Bridging the gap to expose "send(byte[] data)".
* Both on Client and Server * Updated the documented exceptions thrown (there were different mismatches)
1 parent 8669096 commit 40ce57f

3 files changed

Lines changed: 86 additions & 9 deletions

File tree

‎src/net/tootallnate/websocket/WebSocket.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,16 +389,23 @@ public void close( InvalidDataException e ) {
389389
/**
390390
* @return True if all of the text was sent to the client by this thread or the given data is empty
391391
* False if some of the text had to be buffered to be sent later.
392-
* @throws IOException
392+
* @throws IllegalArgumentException
393393
* @throws InterruptedException
394+
* @throws NotYetConnectedException
394395
*/
395396
public void send( String text ) throws IllegalArgumentException , NotYetConnectedException , InterruptedException {
396397
if( text == null )
397398
throw new IllegalArgumentException( "Cannot send 'null' data to a WebSocket." );
398399
send( draft.createFrames( text, role == Role.CLIENT ) );
399400
}
400401

401-
// TODO there should be a send for bytebuffers
402+
/**
403+
* @return True if all of the bytes was sent to the client by this thread or the given data is empty
404+
* False if some of the text had to be buffered to be sent later.
405+
* @throws IllegalArgumentException
406+
* @throws InterruptedException
407+
* @throws NotYetConnectedException
408+
*/
402409
public void send( byte[] bytes ) throws IllegalArgumentException , NotYetConnectedException , InterruptedException {
403410
if( bytes == null )
404411
throw new IllegalArgumentException( "Cannot send 'null' data to a WebSocket." );

‎src/net/tootallnate/websocket/WebSocketClient.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ public void send( String text ) throws NotYetConnectedException , InterruptedExc
123123
}
124124
}
125125

126+
/**
127+
* Sends <var>data</var> to the connected WebSocket server.
128+
*
129+
* @param data
130+
* The Byte-Array of data to send to the WebSocket server.
131+
*/
132+
public void send( byte[] data ) throws NotYetConnectedException , InterruptedException {
133+
if( conn != null ) {
134+
conn.send( data );
135+
}
136+
}
137+
126138
private void tryToConnect( InetSocketAddress remote ) throws IOException {
127139
client = SocketChannel.open();
128140
client.configureBlocking( false );

‎src/net/tootallnate/websocket/WebSocketServer.java‎

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public WebSocketServer() throws UnknownHostException {
6060
/**
6161
* Creates a WebSocketServer that will attempt to listen on port
6262
* <var>port</var>.
63-
*
63+
*
6464
* @param port
6565
* The port number this server should listen on.
6666
*/
@@ -71,7 +71,7 @@ public WebSocketServer( InetSocketAddress address ) {
7171
/**
7272
* Creates a WebSocketServer that will attempt to listen on port <var>port</var>,
7373
* and comply with <tt>Draft</tt> version <var>draft</var>.
74-
*
74+
*
7575
* @param port
7676
* The port number this server should listen on.
7777
* @param draft
@@ -99,7 +99,7 @@ public void start() {
9999
* Closes all connected clients sockets, then closes the underlying
100100
* ServerSocketChannel, effectively killing the server socket thread and
101101
* freeing the port the server was bound to.
102-
*
102+
*
103103
* @throws IOException
104104
* When socket related I/O errors occur.
105105
*/
@@ -114,10 +114,10 @@ public void stop() throws IOException {
114114

115115
/**
116116
* Sends <var>text</var> to all currently connected WebSocket clients.
117-
*
117+
*
118118
* @param text
119119
* The String to send across the network.
120-
* @throws IOException
120+
* @throws InterruptedException
121121
* When socket related I/O errors occur.
122122
*/
123123
public void sendToAll( String text ) throws InterruptedException {
@@ -126,10 +126,24 @@ public void sendToAll( String text ) throws InterruptedException {
126126
}
127127
}
128128

129+
/**
130+
* Sends <var>data</var> to all currently connected WebSocket clients.
131+
*
132+
* @param data
133+
* The Byte-array of data to send across the network.
134+
* @throws InterruptedException
135+
* When socket related I/O errors occur.
136+
*/
137+
public void sendToAll( byte[] data ) throws InterruptedException {
138+
for( WebSocket c : this.connections ) {
139+
c.send( data );
140+
}
141+
}
142+
129143
/**
130144
* Sends <var>text</var> to all currently connected WebSocket clients,
131145
* except for the specified <var>connection</var>.
132-
*
146+
*
133147
* @param connection
134148
* The {@link WebSocket} connection to ignore.
135149
* @param text
@@ -149,10 +163,33 @@ public void sendToAllExcept( WebSocket connection, String text ) throws Interrup
149163
}
150164
}
151165

166+
/**
167+
* Sends <var>data</var> to all currently connected WebSocket clients,
168+
* except for the specified <var>connection</var>.
169+
*
170+
* @param connection
171+
* The {@link WebSocket} connection to ignore.
172+
* @param data
173+
* The Byte-Array of data to send to every connection except <var>connection</var>.
174+
* @throws InterruptedException
175+
* When socket related I/O errors occur.
176+
*/
177+
public void sendToAllExcept( WebSocket connection, byte[] data ) throws InterruptedException {
178+
if( connection == null ) {
179+
throw new NullPointerException( "'connection' cannot be null" );
180+
}
181+
182+
for( WebSocket c : this.connections ) {
183+
if( !connection.equals( c ) ) {
184+
c.send( data );
185+
}
186+
}
187+
}
188+
152189
/**
153190
* Sends <var>text</var> to all currently connected WebSocket clients,
154191
* except for those found in the Set <var>connections</var>.
155-
*
192+
*
156193
* @param connections
157194
* @param text
158195
* @throws IOException
@@ -170,6 +207,27 @@ public void sendToAllExcept( Set<WebSocket> connections, String text ) throws In
170207
}
171208
}
172209

210+
/**
211+
* Sends <var>data</var> to all currently connected WebSocket clients,
212+
* except for those found in the Set <var>connections</var>.
213+
*
214+
* @param connections
215+
* @param data
216+
* @throws InterruptedException
217+
* When socket related I/O errors occur.
218+
*/
219+
public void sendToAllExcept( Set<WebSocket> connections, byte[] data ) throws InterruptedException {
220+
if( connections == null ) {
221+
throw new NullPointerException( "'connections' cannot be null" );
222+
}
223+
224+
for( WebSocket c : this.connections ) {
225+
if( !connections.contains( c ) ) {
226+
c.send( data );
227+
}
228+
}
229+
}
230+
173231
/**
174232
* Returns a WebSocket[] of currently connected clients.
175233
*

0 commit comments

Comments
 (0)