public abstract class WebSocketServer extends AbstractWebSocket implements Runnable
| 限定符和类型 | 类和说明 |
|---|---|
class |
WebSocketServer.WebSocketWorker
This class is used to process incoming data
|
| 限定符和类型 | 字段和说明 |
|---|---|
protected List<WebSocketServer.WebSocketWorker> |
decoders |
static int |
DECODERS |
| 构造器和说明 |
|---|
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) |
| 限定符和类型 | 方法和说明 |
|---|---|
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() |
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.
|
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 |
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()
DECODERS
Alternatively you can call run() directly.
IllegalStateException - Starting an instance againpublic void stop(int timeout)
throws InterruptedException
If this method is called before the server is started it will never start.
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
public Collection<WebSocket> getConnections()
getConnections 在类中 AbstractWebSocketpublic InetSocketAddress getAddress()
public int getPort()
protected void allocateBuffers(WebSocket c) throws InterruptedException
protected void releaseBuffers(WebSocket c) throws InterruptedException
public ByteBuffer createBuffer()
protected void queue(WebSocketImpl ws) throws InterruptedException
public final void onWebsocketMessage(WebSocket conn, String message)
WebSocketListeneronWebsocketMessage 在接口中 WebSocketListenerconn - 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)
WebSocketListeneronWebsocketMessage 在接口中 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 在接口中 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 在接口中 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 在接口中 WebSocketListeneronWebsocketHandshakeReceivedAsServer 在类中 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 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.ex - The exception that occurred. public final void onWriteDemand(WebSocket w)
WebSocketListeneronWriteDemand 在接口中 WebSocketListenerw - The WebSocket instance this event is occuring on.public void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
WebSocketListeneronWebsocketCloseInitiated 在接口中 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 在接口中 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 在接口中 WebSocketListenerconn - The WebSocket instance this event is occuring on.WebSocket.getLocalSocketAddress()public InetSocketAddress getRemoteSocketAddress(WebSocket conn)
getRemoteSocketAddress 在接口中 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()
If any error occured, onError is called instead.
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 © 2020. All rights reserved.