WebSocketServer is an abstract class that only takes care of the
HTTP handshake portion of WebSockets. It's up to a subclass to add functionality/purpose to the
server.
| Modifier and Type | Class and Description |
|---|---|
| public class | WebSocketServer.
This class is used to process incoming data |
| Modifier and Type | Field and Description |
|---|---|
| private final InetSocketAddress | address
The port number that this WebSocket server should listen on. |
| private static final int | |
| private BlockingQueue | |
| private final Collection | connections
Holds the list of active WebSocket connections. |
| protected List | |
| private List | drafts
The Draft of the WebSocket protocol the Server is adhering to. |
| private List | |
| private final AtomicBoolean | |
| private final Logger | log
Logger instance
|
| private int | maxPendingConnections
Attribute which allows you to configure the socket "backlog" parameter which determines how many client connections can be queued. |
| private int | |
| private final AtomicInteger | |
| private Selector | selector
The 'Selector' used to get event keys from the underlying socket. |
| private Thread | |
| private ServerSocketChannel | server
The socket channel for this WebSocket server. |
| private WebSocketServerFactory |
| Access | Constructor and Description |
|---|---|
| public | WebSocketServer()
Creates a WebSocketServer that will attempt to listen on port WebSocketImpl.DEFAULT_PORT. |
| public | WebSocketServer(InetSocketAddress
The address to listen to address)Creates a WebSocketServer that will attempt to bind/listen on the given address. |
| public | WebSocketServer(InetSocketAddress
The address (host:port) this server should listen on. address, int The number of decodercount)WebSocketWorkers that will be used to process the
incoming network data. By default this will be Runtime.getRuntime().availableProcessors()
|
| public | WebSocketServer(InetSocketAddress
The address (host:port) this server should listen on. address, List<Draft> The versions of the WebSocket protocol that this server instance should comply
to. Clients that use an other protocol version will be rejected. drafts)
|
| public | WebSocketServer(InetSocketAddress
The address (host:port) this server should listen on. address, int The number of decodercount, List<Draft> WebSocketWorkers that will be used to process the
incoming network data. By default this will be Runtime.getRuntime().availableProcessors()The versions of the WebSocket protocol that this server instance should
comply to. Clients that use an other protocol version will be rejected. drafts)
|
| public | WebSocketServer(ServerSocketChannel
An already open and bound server socket channel, which this server will use.
For example, it can be System.inheritedChannel() to implement socket activation. existingChannel)
|
| public | WebSocketServer(InetSocketAddress
The address (host:port) this server should listen on. address, int The number of decodercount, List<Draft> WebSocketWorkers that will be used to process
the incoming network data. By default this will be
Runtime.getRuntime().availableProcessors()The versions of the WebSocket protocol that this server instance
should comply to. Clients that use an other protocol version will
be rejected. drafts, Collection<WebSocket> Allows to specify a collection that will be used to store the
websockets in. connectionscontainer)If you plan to often iterate through the currently connected websockets you may want to use a collection that does not require synchronization like a CopyOnWriteArraySet. In that case make sure that you overload
removeConnection(WebSocket) and addConnection(WebSocket).By default a HashSet will
be used.Creates a WebSocketServer that will attempt to bind/listen on the given address, and
comply with |
| Modifier and Type | Method and Description |
|---|---|
| protected boolean | Returns: Adding connection successfulthe Websocket connection which should be added ws)
|
| protected void | |
| public void | |
| public void | |
| public void | broadcast(ByteBuffer
the data to send to the endpoints data)Send a ByteBuffer to all connected endpoints |
| public void | broadcast(byte[]
the data to send to the endpoints data, Collection<WebSocket> a collection of endpoints to whom the text has to be send clients)Send a byte array to a specific collection of websocket connections |
| public void | broadcast(ByteBuffer
the data to send to the endpoints data, Collection<WebSocket> a collection of endpoints to whom the text has to be send clients)Send a ByteBuffer to a specific collection of websocket connections |
| public void | broadcast(String
the text to send to the endpoints text, Collection<WebSocket> a collection of endpoints to whom the text has to be send clients)Send a text to a specific collection of websocket connections |
| private static InetSocketAddress | |
| public ByteBuffer | |
| private void | doAccept(SelectionKey
the selectionkey to read off key, Iterator<SelectionKey> the iterator for the selection keys i)Execute a accept operation |
| private void | |
| private void | doBroadcast(Object
the data to broadcast data, Collection<WebSocket> the clients to send the message to clients)Private method to cache all the frames to improve memory footprint and conversion time |
| private boolean | Returns: true, if the server can be started, false if already a thread is runningThe websocket server can only be started once |
| private boolean | Returns: true, if the read was successful, or false if there was an errorthe selectionkey to read off key, Iterator<SelectionKey> the iterator for the selection keys i)Execute a read operation |
| private void | |
| private boolean | Returns: true, if everything was successful, false if some error happenedSetup the selector thread as well as basic server settings |
| private void | |
| private void | fillFrames(Draft
The draft to use draft, Map<Draft, List<Framedata>> The list of frames per draft to fill draftFrames, String the string data, can be null strData, ByteBuffer the byte buffer data, can be null byteData)Fills the draftFrames with new data for the broadcast |
| public InetSocketAddress | |
| public Collection | Returns: A unmodifiable collection of all currently connected clientsImplements abstract org. Returns all currently connected clients. |
| public List | |
| public InetSocketAddress | |
| public int | Returns: the maximum number of pending connectionsReturns the currently configured maximum number of pending connections. |
| public int | |
| public InetSocketAddress | |
| private Socket | |
| public final WebSocketFactory | |
| private void | |
| private void | |
| public abstract void | onClose(WebSocket
The conn, int WebSocket instance this event is occurring on.The codes can be looked up here: code, String CloseFrameAdditional information string reason, boolean Returns whether or not the closing of the connection was initiated by the remote
host. remote)Called after the websocket connection has been closed. |
| public void | |
| public void | |
| protected boolean | Returns: Can this new connection be acceptedthe SelectionKey for the new connection key)Returns whether a new connection shall be accepted or not. |
| public abstract void | |
| public abstract void | |
| public void | onMessage(WebSocket
The conn, ByteBuffer WebSocket instance this event is occurring on.The binary message that was received. message)Callback for binary messages received from the remote host |
| public abstract void | onOpen(WebSocket
The conn, ClientHandshake WebSocket instance this event is occurring on.The handshake of the websocket instance handshake)Called after an opening handshake has been performed and the given websocket is ready to be written on. |
| public abstract void | |
| public final void | onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
Implements org. Called after |
| public void | onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
Implements org. send when this peer sends a close handshake |
| public void | onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
Implements org. Called as soon as no further frames are accepted |
| public final void | onWebsocketError(WebSocket conn, Exception ex)
Implements org. Called if an exception worth noting occurred. |
| public final void | onWebsocketMessage(WebSocket conn, String message)
Implements org. Called when an entire text frame has been received. |
| public final void | onWebsocketMessage(WebSocket conn, ByteBuffer blob)
Implements org. Called when an entire binary frame has been received. |
| public final void | onWebsocketOpen(WebSocket conn, Handshakedata handshake)
Implements org. Called after onHandshakeReceived returns true. |
| public final void | onWriteDemand(WebSocket w)
Implements org. This method is used to inform the selector thread that there is data queued to be written to the socket. |
| private void | |
| protected void | |
| protected void | |
| protected boolean | Returns: Removing connection successfulThe Websocket connection which should be removed ws)This method performs remove operations on the connection and therefore also gives control over whether the operation shall be synchronized
|
| public void | |
| public void | setDaemon(boolean daemon)
Overrides org. Setter for daemon Controls whether or not internal threads are spawned in daemon mode |
| public void | setMaxPendingConnections(int
the new number of allowed pending connections numberOfConnections)Set the requested maximum number of pending connections on the socket. |
| public final void | |
| public void | start()
Starts the server selectorthread that binds to the currently set port number and listeners for WebSocket connection requests. |
| public void | |
| public void | stop(int
Specifies how many milliseconds the overall close handshaking may take
altogether before the connections are closed without proper close
handshaking. timeout, String Specifies message for remote client closeMessage)Closes all connected clients sockets, then closes the underlying ServerSocketChannel, effectively killing the server socket selectorthread, freeing the port the server was bound to and stops all internal workerthreads. |
| public void | |
| private ByteBuffer |