Skip to content

Commit 153cdf3

Browse files
committed
Added new method onConnect which allows to decide whether or not a connection shall be accepted.
Also did some documentation.
1 parent 122fce7 commit 153cdf3

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

src/org/java_websocket/WebSocketListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
public interface WebSocketListener {
1919

2020
/**
21-
* Called on the server side when the socket connection is first established, and the WebSocketImpl
22-
* handshake has been received.
21+
* Called on the server side when the socket connection is first established, and the WebSocket
22+
* handshake has been received. This method allows to deny connections based on the received handshake.<br>
23+
* By behavior this method only requires protocol compliance.
2324
*
2425
* @param conn
2526
* The WebSocket related to this event

src/org/java_websocket/server/WebSocketServer.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ public void run() {
264264
}
265265

266266
if( key.isAcceptable() ) {
267+
if( !onConnect( key ) ) {
268+
key.cancel();
269+
continue;
270+
}
271+
267272
SocketChannel channel = server.accept();
268273
channel.configureBlocking( false );
269274
WebSocketImpl w = wsf.createWebSocket( this, drafts, channel.socket() );
@@ -310,11 +315,11 @@ public void run() {
310315
conn = iqueue.remove( 0 );
311316
WrappedByteChannel c = ( (WrappedByteChannel) conn.channel );
312317
ByteBuffer buf = takeBuffer();
313-
if(SocketChannelIOHelper.readMore( buf, conn, c ))
318+
if( SocketChannelIOHelper.readMore( buf, conn, c ) )
314319
iqueue.add( conn );
315320
conn.inQueue.put( buf );
316321
queue( conn );
317-
322+
318323
}
319324
} catch ( CancelledKeyException e ) {
320325
// an other thread may cancel the key
@@ -473,11 +478,49 @@ public final WebSocketFactory getWebSocketFactory() {
473478
return wsf;
474479
}
475480

476-
// ABTRACT METHODS /////////////////////////////////////////////////////////
481+
/**
482+
* Returns whether a new connection shall be accepted or not.<br>
483+
* Therefore method is well suited to implement some kind of connection limitation.<br>
484+
*
485+
* @see {@link #onOpen(WebSocket, ClientHandshake)}, {@link #onWebsocketHandshakeReceivedAsServer(WebSocket, Draft, ClientHandshake)}
486+
**/
487+
protected boolean onConnect( SelectionKey key ) {
488+
return true;
489+
}
490+
491+
/** Called after an opening handshake has been performed and the given websocket is ready to be written on. */
477492
public abstract void onOpen( WebSocket conn, ClientHandshake handshake );
493+
/**
494+
* Called after the websocket connection has been closed.
495+
*
496+
* @param code
497+
* The codes can be looked up here: {@link CloseFrame}
498+
* @param reason
499+
* Additional information string
500+
* @param remote
501+
* Returns whether or not the closing of the connection was initiated by the remote host.
502+
**/
478503
public abstract void onClose( WebSocket conn, int code, String reason, boolean remote );
504+
/**
505+
* Callback for string messages received from the remote host
506+
*
507+
* @see #onMessage(WebSocket, ByteBuffer)
508+
**/
479509
public abstract void onMessage( WebSocket conn, String message );
510+
/**
511+
* Called when errors occurs. If an error causes the websocket connection to fail {@link #onClose(WebSocket, int, String, boolean)} will be called additionally.<br>
512+
* This method will be called primarily because of IO or protocol errors.<br>
513+
* If the given exception is an RuntimeException that probably means that you encountered a bug.<br>
514+
*
515+
* @param con
516+
* Can be null if there error does not belong to one specific websocket. For example if the servers port could not be bound.
517+
**/
480518
public abstract void onError( WebSocket conn, Exception ex );
519+
/**
520+
* Callback for binary messages received from the remote host
521+
*
522+
* @see #onMessage(WebSocket, String)
523+
**/
481524
public void onMessage( WebSocket conn, ByteBuffer message ) {
482525
};
483526

0 commit comments

Comments
 (0)