public abstract class WebSocketServer extends WebSocketAdapter implements Runnable
Modifier and Type | Class and Description |
---|---|
static interface |
WebSocketServer.WebSocketServerFactory |
class |
WebSocketServer.WebSocketWorker |
Modifier and Type | Field and Description |
---|---|
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 decoders) |
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) |
Collection<WebSocket> |
connections()
Returns a WebSocket[] of currently connected clients.
|
ByteBuffer |
createBuffer() |
InetSocketAddress |
getAddress() |
List<Draft> |
getDraft() |
protected String |
getFlashSecurityPolicy()
Gets the XML string that should be returned if a client requests a Flash
security policy.
|
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.
Therefore method is well suited to implement some kind of connection limitation. |
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.
|
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 |
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
|
void |
run() |
void |
setWebSocketFactory(WebSocketServer.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.
|
getFlashPolicy, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketMessageFragment, onWebsocketPing, onWebsocketPong
public WebSocketServer() throws UnknownHostException
UnknownHostException
more details here
public WebSocketServer(InetSocketAddress address)
more details here
public WebSocketServer(InetSocketAddress address, int decoders)
more details here
public WebSocketServer(InetSocketAddress address, List<Draft> drafts)
more details here
public WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts)
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()
DECODERS
run()
directly.IllegalStateException
public void stop(int timeout) throws IOException, InterruptedException
timeout
- Specifies how many milliseconds shall pass between initiating the close handshakes with the connected clients and closing the servers socket channel.IOException
- When ServerSocketChannel
.close throws an IOExceptionInterruptedException
public void stop() throws IOException, InterruptedException
IOException
InterruptedException
public Collection<WebSocket> connections()
public InetSocketAddress getAddress()
public int getPort()
protected void allocateBuffers(WebSocket c) throws InterruptedException
InterruptedException
protected void releaseBuffers(WebSocket c) throws InterruptedException
InterruptedException
public ByteBuffer createBuffer()
protected String getFlashSecurityPolicy()
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.public final void onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
WebSocketListener
onWebsocketClose
in interface WebSocketListener
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.
protected boolean addConnection(WebSocket ws)
removeConnection(WebSocket)
public final void onWebsocketError(WebSocket conn, Exception ex)
WebSocketListener
onWebsocketError
in interface WebSocketListener
conn
- may be null if the error does not belong to a single connectionex
- The exception that occurred. public final void onWriteDemand(WebSocket w)
WebSocketListener
onWriteDemand
in interface WebSocketListener
public void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
WebSocketListener
onWebsocketCloseInitiated
in interface WebSocketListener
public void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
WebSocketListener
onWebsocketClosing
in interface WebSocketListener
public final void setWebSocketFactory(WebSocketServer.WebSocketServerFactory wsf)
public final WebSocketFactory getWebSocketFactory()
protected boolean onConnect(SelectionKey key)
#onOpen(WebSocket, ClientHandshake)}, {@link #onWebsocketHandshakeReceivedAsServer(WebSocket, Draft, ClientHandshake)}
public InetSocketAddress getLocalSocketAddress(WebSocket conn)
getLocalSocketAddress
in interface WebSocketListener
public InetSocketAddress getRemoteSocketAddress(WebSocket conn)
getRemoteSocketAddress
in interface WebSocketListener
public abstract void onOpen(WebSocket conn, ClientHandshake handshake)
public abstract void onClose(WebSocket conn, int code, String reason, boolean remote)
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)
onMessage(WebSocket, ByteBuffer)
public abstract void onError(WebSocket conn, Exception ex)
onClose(WebSocket, int, String, boolean)
will be called additionally.con
- Can be null if there error does not belong to one specific websocket. For example if the servers port could not be bound.public void onMessage(WebSocket conn, ByteBuffer message)
onMessage(WebSocket, String)
Copyright © 2013. All Rights Reserved.