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 |
static int |
DECODERS |
| Constructor and Description |
|---|
WebSocketServer()
Creates a WebSocketServer that will attempt to
listen on port WebSocket.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(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
|
Collection<WebSocket> |
connections()
Returns a WebSocket[] of currently connected clients.
|
ByteBuffer |
createBuffer() |
InetSocketAddress |
getAddress() |
List<Draft> |
getDraft() |
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 |
onFragment(WebSocket conn,
Framedata fragment)
Deprecated.
|
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.
|
ServerHandshakeBuilder |
onWebsocketHandshakeReceivedAsServer(WebSocket conn,
Draft draft,
ClientHandshake request)
This default implementation does not do anything.
|
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 |
onWebsocketMessageFragment(WebSocket conn,
Framedata frame)
Deprecated.
|
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, stopConnectionLostTimeronWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPongpublic static int DECODERS
protected List<WebSocketServer.WebSocketWorker> decoders
public WebSocketServer()
more details herepublic WebSocketServer(InetSocketAddress address)
address - The address to listen tomore details herepublic WebSocketServer(InetSocketAddress address, int decodercount)
address - The address (host:port) this server should listen on.decodercount - The number of WebSocketServer.WebSocketWorkers that will be used to process the incoming network data. By default this will be Runtime.getRuntime().availableProcessors()more details herepublic 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 herepublic WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts)
address - The address (host:port) this server should listen on.decodercount - The number of WebSocketServer.WebSocketWorkers 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 herepublic 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.WebSocketWorkers 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()
DECODERSrun() 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
IOExceptionInterruptedExceptionpublic Collection<WebSocket> connections()
connections in class AbstractWebSocketpublic InetSocketAddress getAddress()
public int getPort()
protected void allocateBuffers(WebSocket c) throws InterruptedException
InterruptedExceptionprotected void releaseBuffers(WebSocket c) throws InterruptedException
InterruptedExceptionpublic ByteBuffer createBuffer()
protected void queue(WebSocketImpl ws) throws InterruptedException
InterruptedExceptionpublic final void onWebsocketMessage(WebSocket conn, String message)
WebSocketListeneronWebsocketMessage in interface WebSocketListenerconn - The WebSocket instance this event is occurring on.message - The UTF-8 decoded message that was received.@Deprecated public void onWebsocketMessageFragment(WebSocket conn, Framedata frame)
WebSocketAdapteronWebsocketMessageFragment in interface WebSocketListeneronWebsocketMessageFragment in class WebSocketAdapterconn - The WebSocket instance this event is occurring on.frame - The fragmented frameWebSocketListener.onWebsocketMessageFragment(WebSocket, Framedata)public final void onWebsocketMessage(WebSocket conn, ByteBuffer blob)
WebSocketListeneronWebsocketMessage in interface WebSocketListenerconn - The WebSocket instance this event is occurring on.blob - The binary message that was received.public final void onWebsocketOpen(WebSocket conn, Handshakedata handshake)
WebSocketListeneronWebsocketOpen in interface WebSocketListenerconn - 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)
WebSocketListeneronWebsocketClose in interface WebSocketListenerconn - The WebSocket instance this event is occuring on.code - The codes can be looked up here: CloseFramereason - 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 Webscoket connection which should be removedpublic ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket conn, Draft draft, ClientHandshake request) throws InvalidDataException
WebSocketAdapteronWebsocketHandshakeReceivedAsServer in interface WebSocketListeneronWebsocketHandshakeReceivedAsServer in class WebSocketAdapterconn - The WebSocket related to this eventdraft - The protocol draft the client uses to connectrequest - The opening http message send by the client. Can be used to access additional fields like cookies.InvalidDataException - Throwing this exception will cause this handshake to be rejectedWebSocketListener.onWebsocketHandshakeReceivedAsServer(WebSocket, Draft, ClientHandshake)protected boolean addConnection(WebSocket ws)
ws - the Webscoket connection which should be addedremoveConnection(WebSocket)public final void onWebsocketError(WebSocket conn, Exception ex)
WebSocketListeneronWebsocketError in interface WebSocketListenerconn - The WebSocket instance this event is occuring on.ex - The exception that occurred. public final void onWriteDemand(WebSocket w)
WebSocketListeneronWriteDemand in interface WebSocketListenerw - The WebSocket instance this event is occuring on.public void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
WebSocketListeneronWebsocketCloseInitiated in interface WebSocketListenerconn - The WebSocket instance this event is occuring on.code - The codes can be looked up here: CloseFramereason - Additional information stringpublic void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
WebSocketListeneronWebsocketClosing in interface WebSocketListenerconn - The WebSocket instance this event is occuring on.code - The codes can be looked up here: CloseFramereason - 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),
onWebsocketHandshakeReceivedAsServer(WebSocket, Draft, ClientHandshake)public InetSocketAddress getLocalSocketAddress(WebSocket conn)
getLocalSocketAddress in interface WebSocketListenerconn - The WebSocket instance this event is occuring on.WebSocket.getLocalSocketAddress()public InetSocketAddress getRemoteSocketAddress(WebSocket conn)
getRemoteSocketAddress in interface WebSocketListenerconn - 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: CloseFramereason - 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)@Deprecated public void onFragment(WebSocket conn, Framedata fragment)
conn - The WebSocket instance this event is occurring on.fragment - The fragmented frameWebSocket.sendFragmentedFrame(org.java_websocket.framing.Framedata.Opcode, ByteBuffer, boolean)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(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(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 © 2017. All rights reserved.