public abstract class WebSocketServer extends AbstractWebSocket implements Runnable
Modifier and Type | Class and Description |
---|---|
class |
WebSocketServer.WebSocketWorker
This class is used to process incoming data
|
Modifier and Type | Field and Description |
---|---|
protected List<WebSocketServer.WebSocketWorker> |
decoders |
Constructor and Description |
---|
WebSocketServer()
Creates a WebSocketServer that will attempt to
listen on port WebSocketImpl.DEFAULT_PORT.
|
WebSocketServer(InetSocketAddress address)
Creates a WebSocketServer that will attempt to bind/listen on the given address.
|
WebSocketServer(InetSocketAddress address,
int decodercount) |
WebSocketServer(InetSocketAddress address,
int decodercount,
List<Draft> drafts) |
WebSocketServer(InetSocketAddress address,
int decodercount,
List<Draft> drafts,
Collection<WebSocket> connectionscontainer)
Creates a WebSocketServer that will attempt to bind/listen on the given address,
and comply with Draft version draft.
|
WebSocketServer(InetSocketAddress address,
List<Draft> drafts) |
Modifier and Type | Method and Description |
---|---|
protected boolean |
addConnection(WebSocket ws) |
protected void |
allocateBuffers(WebSocket c) |
void |
broadcast(byte[] data)
Send a byte array to all connected endpoints
|
void |
broadcast(byte[] data,
Collection<WebSocket> clients)
Send a byte array to a specific collection of websocket connections
|
void |
broadcast(ByteBuffer data)
Send a ByteBuffer to all connected endpoints
|
void |
broadcast(ByteBuffer data,
Collection<WebSocket> clients)
Send a ByteBuffer to a specific collection of websocket connections
|
void |
broadcast(String text)
Send a text to all connected endpoints
|
void |
broadcast(String text,
Collection<WebSocket> clients)
Send a text to a specific collection of websocket connections
|
ByteBuffer |
createBuffer() |
InetSocketAddress |
getAddress() |
Collection<WebSocket> |
getConnections()
Returns all currently connected clients.
|
List<Draft> |
getDraft()
Get the list of active drafts
|
InetSocketAddress |
getLocalSocketAddress(WebSocket conn) |
int |
getPort()
Gets the port number that this server listens on.
|
InetSocketAddress |
getRemoteSocketAddress(WebSocket conn) |
WebSocketFactory |
getWebSocketFactory() |
abstract void |
onClose(WebSocket conn,
int code,
String reason,
boolean remote)
Called after the websocket connection has been closed.
|
void |
onCloseInitiated(WebSocket conn,
int code,
String reason) |
void |
onClosing(WebSocket conn,
int code,
String reason,
boolean remote) |
protected boolean |
onConnect(SelectionKey key)
Returns whether a new connection shall be accepted or not.
|
abstract void |
onError(WebSocket conn,
Exception ex)
Called when errors occurs.
|
void |
onMessage(WebSocket conn,
ByteBuffer message)
Callback for binary messages received from the remote host
|
abstract void |
onMessage(WebSocket conn,
String message)
Callback for string messages received from the remote host
|
abstract void |
onOpen(WebSocket conn,
ClientHandshake handshake)
Called after an opening handshake has been performed and the given websocket is ready to be written on.
|
abstract void |
onStart()
Called when the server started up successfully.
|
void |
onWebsocketClose(WebSocket conn,
int code,
String reason,
boolean remote)
Called after WebSocket#close is explicity called, or when the
other end of the WebSocket connection is closed.
|
void |
onWebsocketCloseInitiated(WebSocket conn,
int code,
String reason)
send when this peer sends a close handshake
|
void |
onWebsocketClosing(WebSocket conn,
int code,
String reason,
boolean remote)
Called as soon as no further frames are accepted
|
void |
onWebsocketError(WebSocket conn,
Exception ex)
Called if an exception worth noting occurred.
|
void |
onWebsocketMessage(WebSocket conn,
ByteBuffer blob)
Called when an entire binary frame has been received.
|
void |
onWebsocketMessage(WebSocket conn,
String message)
Called when an entire text frame has been received.
|
void |
onWebsocketOpen(WebSocket conn,
Handshakedata handshake)
Called after onHandshakeReceived returns true.
|
void |
onWriteDemand(WebSocket w)
This method is used to inform the selector thread that there is data queued to be written to the socket.
|
protected void |
queue(WebSocketImpl ws) |
protected void |
releaseBuffers(WebSocket c) |
protected boolean |
removeConnection(WebSocket ws)
This method performs remove operations on the connection and therefore also gives control over whether the operation shall be synchronized
WebSocketServer(InetSocketAddress, int, List, Collection) allows to specify a collection which will be used to store current connections in. |
void |
run() |
void |
setWebSocketFactory(WebSocketServerFactory wsf) |
void |
start()
Starts the server selectorthread that binds to the currently set port number and
listeners for WebSocket connection requests.
|
void |
stop() |
void |
stop(int timeout)
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.
|
getConnectionLostTimeout, isReuseAddr, isTcpNoDelay, setConnectionLostTimeout, setReuseAddr, setTcpNoDelay, startConnectionLostTimer, stopConnectionLostTimer
onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPong
protected List<WebSocketServer.WebSocketWorker> decoders
public WebSocketServer()
more details here
public WebSocketServer(InetSocketAddress address)
address
- The address to listen tomore details here
public WebSocketServer(InetSocketAddress address, int decodercount)
address
- The address (host:port) this server should listen on.decodercount
- The number of WebSocketServer.WebSocketWorker
s that will be used to process the incoming network data. By default this will be Runtime.getRuntime().availableProcessors()
more details here
public WebSocketServer(InetSocketAddress address, List<Draft> drafts)
address
- The address (host:port) this server should listen on.drafts
- The versions of the WebSocket protocol that this server
instance should comply to. Clients that use an other protocol version will be rejected.more details here
public WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts)
address
- The address (host:port) this server should listen on.decodercount
- The number of WebSocketServer.WebSocketWorker
s that will be used to process the incoming network data. By default this will be Runtime.getRuntime().availableProcessors()
drafts
- The versions of the WebSocket protocol that this server
instance should comply to. Clients that use an other protocol version will be rejected.more details here
public WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts, Collection<WebSocket> connectionscontainer)
address
- The address (host:port) this server should listen on.decodercount
- The number of WebSocketServer.WebSocketWorker
s that will be used to process the incoming network data. By default this will be Runtime.getRuntime().availableProcessors()
drafts
- The versions of the WebSocket protocol that this server
instance should comply to. Clients that use an other protocol version will be rejected.connectionscontainer
- Allows to specify a collection that will be used to store the websockets in. CopyOnWriteArraySet
. In that case make sure that you overload removeConnection(WebSocket)
and addConnection(WebSocket)
.HashSet
will be used.for more control over syncronized operation
,
more about draftspublic void start()
AVAILABLE_PROCESSORS
run()
directly.IllegalStateException
- Starting an instance againpublic void stop(int timeout) throws InterruptedException
timeout
- Specifies how many milliseconds the overall close handshaking may take altogether before the connections are closed without proper close handshaking.InterruptedException
- Interruptpublic void stop() throws IOException, InterruptedException
IOException
InterruptedException
public Collection<WebSocket> getConnections()
getConnections
in class AbstractWebSocket
public InetSocketAddress getAddress()
public int getPort()
public List<Draft> getDraft()
protected void allocateBuffers(WebSocket c) throws InterruptedException
InterruptedException
protected void releaseBuffers(WebSocket c) throws InterruptedException
InterruptedException
public ByteBuffer createBuffer()
protected void queue(WebSocketImpl ws) throws InterruptedException
InterruptedException
public final void onWebsocketMessage(WebSocket conn, String message)
WebSocketListener
onWebsocketMessage
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.message
- The UTF-8 decoded message that was received.public final void onWebsocketMessage(WebSocket conn, ByteBuffer blob)
WebSocketListener
onWebsocketMessage
in interface WebSocketListener
conn
- The WebSocket instance this event is occurring on.blob
- The binary message that was received.public final void onWebsocketOpen(WebSocket conn, Handshakedata handshake)
WebSocketListener
onWebsocketOpen
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.handshake
- The handshake of the websocket instancepublic final void onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
WebSocketListener
onWebsocketClose
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remote host.protected boolean removeConnection(WebSocket ws)
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.
ws
- The Websocket connection which should be removedprotected boolean addConnection(WebSocket ws)
ws
- the Websocket connection which should be addedremoveConnection(WebSocket)
public final void onWebsocketError(WebSocket conn, Exception ex)
WebSocketListener
onWebsocketError
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.ex
- The exception that occurred. public final void onWriteDemand(WebSocket w)
WebSocketListener
onWriteDemand
in interface WebSocketListener
w
- The WebSocket instance this event is occuring on.public void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
WebSocketListener
onWebsocketCloseInitiated
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringpublic void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
WebSocketListener
onWebsocketClosing
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remote host.public final void setWebSocketFactory(WebSocketServerFactory wsf)
public final WebSocketFactory getWebSocketFactory()
protected boolean onConnect(SelectionKey key)
key
- the SelectionKey for the new connectiononOpen(WebSocket, ClientHandshake)
,
WebSocketAdapter.onWebsocketHandshakeReceivedAsServer(WebSocket, Draft, ClientHandshake)
public InetSocketAddress getLocalSocketAddress(WebSocket conn)
getLocalSocketAddress
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.WebSocket.getLocalSocketAddress()
public InetSocketAddress getRemoteSocketAddress(WebSocket conn)
getRemoteSocketAddress
in interface WebSocketListener
conn
- The WebSocket instance this event is occuring on.null
if it is unconnected.WebSocket.getRemoteSocketAddress()
public abstract void onOpen(WebSocket conn, ClientHandshake handshake)
conn
- The WebSocket instance this event is occuring on.handshake
- The handshake of the websocket instancepublic abstract void onClose(WebSocket conn, int code, String reason, boolean remote)
conn
- The WebSocket instance this event is occuring on.code
- The codes can be looked up here: CloseFrame
reason
- Additional information stringremote
- Returns whether or not the closing of the connection was initiated by the remote host.public abstract void onMessage(WebSocket conn, String message)
conn
- The WebSocket instance this event is occuring on.message
- The UTF-8 decoded message that was received.onMessage(WebSocket, ByteBuffer)
public abstract void onError(WebSocket conn, Exception ex)
onClose(WebSocket, int, String, boolean)
will be called additionally.conn
- Can be null if there error does not belong to one specific websocket. For example if the servers port could not be bound.ex
- The exception causing this errorpublic abstract void onStart()
public void onMessage(WebSocket conn, ByteBuffer message)
conn
- The WebSocket instance this event is occurring on.message
- The binary message that was received.onMessage(WebSocket, ByteBuffer)
public void broadcast(String text)
text
- the text to send to the endpointspublic void broadcast(byte[] data)
data
- the data to send to the endpointspublic void broadcast(ByteBuffer data)
data
- the data to send to the endpointspublic void broadcast(byte[] data, Collection<WebSocket> clients)
data
- the data to send to the endpointsclients
- a collection of endpoints to whom the text has to be sendpublic void broadcast(ByteBuffer data, Collection<WebSocket> clients)
data
- the data to send to the endpointsclients
- a collection of endpoints to whom the text has to be sendpublic void broadcast(String text, Collection<WebSocket> clients)
text
- the text to send to the endpointsclients
- a collection of endpoints to whom the text has to be sendCopyright © 2019. All rights reserved.