Runtime.getRuntime().availableProcessors()
*/
public WebSocketServer( InetSocketAddress address , int decodercount ) {
this( address, decodercount, null );
}
/**
* @see #WebSocketServer(InetSocketAddress, int, List, Collection) more details here
*
* @param address
* The address (host:port) this server should listen on.
* @param drafts
* The versions of the WebSocket protocol that this server
* instance should comply to. Clients that use an other protocol version will be rejected.
*
*/
public WebSocketServer( InetSocketAddress address , ListRuntime.getRuntime().availableProcessors()
* @param drafts
* The versions of the WebSocket protocol that this server
* instance should comply to. Clients that use an other protocol version will be rejected.
*/
public WebSocketServer( InetSocketAddress address , int decodercount , ListRuntime.getRuntime().availableProcessors()
* @param drafts
* The versions of the WebSocket protocol that this server
* instance should comply to. Clients that use an other protocol version will be rejected.
*
* @param connectionscontainer
* Allows to specify a collection that will be used to store the websockets in.
* {@link #WebSocketServer(InetSocketAddress, int, List, Collection)} allows to specify a collection which will be used to store current connections in.
* Depending on the type on the connection, modifications of that collection may have to be synchronized.
* @param ws The Webscoket connection which should be removed
* @return Removing connection successful
*/
protected boolean removeConnection( WebSocket ws ) {
boolean removed;
synchronized ( connections ) {
removed = this.connections.remove( ws );
assert ( removed );
}
if( isclosed.get() && connections.size() == 0 ) {
selectorthread.interrupt();
}
return removed;
}
@Override
public ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer( WebSocket conn, Draft draft, ClientHandshake request ) throws InvalidDataException {
return super.onWebsocketHandshakeReceivedAsServer( conn, draft, request );
}
/**
* @see #removeConnection(WebSocket)
* @param ws the Webscoket connection which should be added
* @return Adding connection successful
*/
protected boolean addConnection( WebSocket ws ) {
if( !isclosed.get() ) {
synchronized ( connections ) {
boolean succ = this.connections.add( ws );
assert ( succ );
return succ;
}
} else {
// This case will happen when a new connection gets ready while the server is already stopping.
ws.close( CloseFrame.GOING_AWAY );
return true;// for consistency sake we will make sure that both onOpen will be called
}
}
@Override
public final void onWebsocketError( WebSocket conn, Exception ex ) {
onError( conn, ex );
}
@Override
public final void onWriteDemand( WebSocket w ) {
WebSocketImpl conn = (WebSocketImpl) w;
try {
conn.key.interestOps( SelectionKey.OP_READ | SelectionKey.OP_WRITE );
} catch ( CancelledKeyException e ) {
// the thread which cancels key is responsible for possible cleanup
conn.outQueue.clear();
}
selector.wakeup();
}
@Override
public void onWebsocketCloseInitiated( WebSocket conn, int code, String reason ) {
onCloseInitiated( conn, code, reason );
}
@Override
public void onWebsocketClosing( WebSocket conn, int code, String reason, boolean remote ) {
onClosing( conn, code, reason, remote );
}
public void onCloseInitiated( WebSocket conn, int code, String reason ) {
}
public void onClosing( WebSocket conn, int code, String reason, boolean remote ) {
}
public final void setWebSocketFactory( WebSocketServerFactory wsf ) {
this.wsf = wsf;
}
public final WebSocketFactory getWebSocketFactory() {
return wsf;
}
/**
* Returns whether a new connection shall be accepted or not.
* Therefore method is well suited to implement some kind of connection limitation.
*
* @see #onOpen(WebSocket, ClientHandshake)
* @see #onWebsocketHandshakeReceivedAsServer(WebSocket, Draft, ClientHandshake)
* @param key the SelectionKey for the new connection
* @return Can this new connection be accepted
**/
protected boolean onConnect( SelectionKey key ) {
//FIXME
return true;
}
/**
* Getter to return the socket used by this specific connection
* @param conn The specific connection
* @return The socket used by this connection
*/
private Socket getSocket( WebSocket conn ) {
WebSocketImpl impl = (WebSocketImpl) conn;
return ( (SocketChannel) impl.key.channel() ).socket();
}
@Override
public InetSocketAddress getLocalSocketAddress( WebSocket conn ) {
return (InetSocketAddress) getSocket( conn ).getLocalSocketAddress();
}
@Override
public InetSocketAddress getRemoteSocketAddress( WebSocket conn ) {
return (InetSocketAddress) getSocket( conn ).getRemoteSocketAddress();
}
/** Called after an opening handshake has been performed and the given websocket is ready to be written on.
* @param conn The WebSocket instance this event is occuring on.
* @param handshake The handshake of the websocket instance
*/
public abstract void onOpen( WebSocket conn, ClientHandshake handshake );
/**
* Called after the websocket connection has been closed.
*
* @param conn The WebSocket instance this event is occuring on.
* @param code
* The codes can be looked up here: {@link CloseFrame}
* @param reason
* Additional information string
* @param remote
* Returns whether or not the closing of the connection was initiated by the remote host.
**/
public abstract void onClose( WebSocket conn, int code, String reason, boolean remote );
/**
* Callback for string messages received from the remote host
*
* @see #onMessage(WebSocket, ByteBuffer)
* @param conn The WebSocket instance this event is occuring on.
* @param message The UTF-8 decoded message that was received.
**/
public abstract void onMessage( WebSocket conn, String message );
/**
* Called when errors occurs. If an error causes the websocket connection to fail {@link #onClose(WebSocket, int, String, boolean)} will be called additionally.
* This method will be called primarily because of IO or protocol errors.
* If the given exception is an RuntimeException that probably means that you encountered a bug.
*
* @param conn Can be null if there error does not belong to one specific websocket. For example if the servers port could not be bound.
* @param ex The exception causing this error
**/
public abstract void onError( WebSocket conn, Exception ex );
/**
* Called when the server started up successfully.
*
* If any error occured, onError is called instead.
*/
public abstract void onStart();
/**
* Callback for binary messages received from the remote host
*
* @see #onMessage(WebSocket, ByteBuffer)
*
* @param conn
* The WebSocket instance this event is occurring on.
* @param message
* The binary message that was received.
**/
public void onMessage( WebSocket conn, ByteBuffer message ) {
}
/**
* Callback for fragmented frames
* @see WebSocket#sendFragmentedFrame(org.java_websocket.framing.Framedata.Opcode, ByteBuffer, boolean)
* @param conn
* The WebSocket instance this event is occurring on.
* @param fragment The fragmented frame
*/
public void onFragment( WebSocket conn, Framedata fragment ) {
}
/**
* This class is used to process incoming data
*/
public class WebSocketWorker extends Thread {
private BlockingQueue
* @throws IOException may be thrown while writing on the channel
*/
public ByteChannel wrapChannel( SocketChannel channel, SelectionKey key ) throws IOException;
/**
* Allows to shutdown the websocket factory for a clean shutdown
*/
public void close();
}
}