-
- All Implemented Interfaces:
-
com.tds.common.websocket.conn.WebSocketListener
,java.lang.Runnable
public abstract class WebSocketServer extends AbstractWebSocket implements Runnable
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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public class
WebSocketServer.WebSocketWorker
This class is used to process incoming data
-
Field Summary
Fields Modifier and Type Field Description private final Collection<WebSocket>
connections
private final InetSocketAddress
address
private int
maxPendingConnections
-
Constructor Summary
Constructors Constructor Description WebSocketServer()
Creates a WebSocketServer that will attempt to listen on port WebSocketImpl. WebSocketServer(InetSocketAddress address)
Creates a WebSocketServer that will attempt to bind/listen on the given address. WebSocketServer(InetSocketAddress address, int decodercount)
WebSocketServer(InetSocketAddress address, List<Draft> drafts)
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, andcomply with Draft version draft.
-
Method Summary
Modifier and Type Method Description Collection<WebSocket>
getConnections()
Returns all currently connected clients. InetSocketAddress
getAddress()
int
getMaxPendingConnections()
Returns the currently configured maximum number of pending connections. void
setMaxPendingConnections(int numberOfConnections)
Set the requested maximum number of pending connections on the socket. void
start()
Starts the server selectorthread that binds to the currently set port number and listeners forWebSocket connection requests. 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 toand stops all internal workerthreads. void
stop()
int
getPort()
Gets the port number that this server listens on. List<Draft>
getDraft()
Get the list of active drafts void
run()
ByteBuffer
createBuffer()
final void
onWebsocketMessage(WebSocket conn, String message)
Called when an entire text frame has been received. final void
onWebsocketMessage(WebSocket conn, ByteBuffer blob)
Called when an entire binary frame has been received. final void
onWebsocketOpen(WebSocket conn, Handshakedata handshake)
Called after onHandshakeReceived returns true. final void
onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
Called after WebSocket#close is explicity called, or when the other end of theWebSocket connection is closed. final void
onWebsocketError(WebSocket conn, Exception ex)
Called if an exception worth noting occurred. final void
onWriteDemand(WebSocket w)
This method is used to inform the selector thread that there is data queued to be written tothe socket. 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
onCloseInitiated(WebSocket conn, int code, String reason)
void
onClosing(WebSocket conn, int code, String reason, boolean remote)
final void
setWebSocketFactory(WebSocketServerFactory wsf)
final WebSocketFactory
getWebSocketFactory()
InetSocketAddress
getLocalSocketAddress(WebSocket conn)
InetSocketAddress
getRemoteSocketAddress(WebSocket conn)
abstract void
onOpen(WebSocket conn, ClientHandshake handshake)
Called after an opening handshake has been performed and the given websocket is ready to bewritten on. abstract void
onClose(WebSocket conn, int code, String reason, boolean remote)
Called after the websocket connection has been closed. abstract void
onMessage(WebSocket conn, String message)
Callback for string messages received from the remote host abstract void
onError(WebSocket conn, Exception ex)
Called when errors occurs. abstract void
onStart()
Called when the server started up successfully. void
onMessage(WebSocket conn, ByteBuffer message)
Callback for binary messages received from the remote host void
broadcast(String text)
Send a text to all connected endpoints void
broadcast(Array<byte> data)
Send a byte array to all connected endpoints void
broadcast(ByteBuffer data)
Send a ByteBuffer to all connected endpoints void
broadcast(Array<byte> data, Collection<WebSocket> clients)
Send a byte array to a specific collection of websocket connections void
broadcast(ByteBuffer data, Collection<WebSocket> clients)
Send a ByteBuffer to a specific collection of websocket connections void
broadcast(String text, Collection<WebSocket> clients)
Send a text to a specific collection of websocket connections -
Methods inherited from class com.tds.common.websocket.conn.AbstractWebSocket
getConnectionLostTimeout, isReuseAddr, isTcpNoDelay, setConnectionLostTimeout, setReuseAddr, setTcpNoDelay
-
Methods inherited from class com.tds.common.websocket.conn.WebSocketAdapter
onPreparePing, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPong
-
Methods inherited from class com.tds.common.websocket.conn.WebSocketListener
onWebsocketClose, onWebsocketCloseInitiated, onWebsocketClosing, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketOpen, onWriteDemand
-
Methods inherited from class java.lang.Runnable
run
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Constructor Detail
-
WebSocketServer
WebSocketServer()
Creates a WebSocketServer that will attempt to listen on port WebSocketImpl.
-
WebSocketServer
WebSocketServer(InetSocketAddress address)
Creates a WebSocketServer that will attempt to bind/listen on the given address.- Parameters:
address
- The address to listen to
-
WebSocketServer
WebSocketServer(InetSocketAddress address, int decodercount)
- Parameters:
address
- The address (host:port) this server should listen on.decodercount
- The number of WebSocketWorkers that will be used to process theincoming network data.
-
WebSocketServer
WebSocketServer(InetSocketAddress address, List<Draft> drafts)
- Parameters:
address
- The address (host:port) this server should listen on.drafts
- The versions of the WebSocket protocol that this server instance should complyto.
-
WebSocketServer
WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts)
- Parameters:
address
- The address (host:port) this server should listen on.decodercount
- The number of WebSocketWorkers that will be used to process theincoming network data.drafts
- The versions of the WebSocket protocol that this server instance shouldcomply to.
-
WebSocketServer
WebSocketServer(InetSocketAddress address, int decodercount, List<Draft> drafts, Collection<WebSocket> connectionscontainer)
Creates a WebSocketServer that will attempt to bind/listen on the given address, andcomply with Draft version draft.- Parameters:
address
- The address (host:port) this server should listen on.decodercount
- The number of WebSocketWorkers that will be used to processthe incoming network data.drafts
- The versions of the WebSocket protocol that this server instanceshould comply to.connectionscontainer
- Allows to specify a collection that will be used to store thewebsockets in.
-
-
Method Detail
-
getConnections
Collection<WebSocket> getConnections()
Returns all currently connected clients. This collection does not allow any modification e.g.removing a client.
-
getAddress
InetSocketAddress getAddress()
-
getMaxPendingConnections
int getMaxPendingConnections()
Returns the currently configured maximum number of pending connections.
-
setMaxPendingConnections
void setMaxPendingConnections(int numberOfConnections)
Set the requested maximum number of pending connections on the socket. The exact semantics areimplementation specific. The value provided should be greater than 0. If it is less than orequal to 0, then an implementation specific default will be used. This option will be passed as"backlog" parameter to bind
-
start
void start()
Starts the server selectorthread that binds to the currently set port number and listeners forWebSocket connection requests. Creates a fixed thread pool with the size May only be called once.
Alternatively you can call run directly.
-
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 toand stops all internal workerthreads.
If this method is called before the server is started it will never start.
- Parameters:
timeout
- Specifies how many milliseconds the overall close handshaking may takealtogether before the connections are closed without proper closehandshaking.
-
stop
void stop()
-
getPort
int getPort()
Gets the port number that this server listens on.
-
run
void run()
-
createBuffer
ByteBuffer createBuffer()
-
onWebsocketMessage
final void onWebsocketMessage(WebSocket conn, String message)
Called when an entire text frame has been received. Do whatever you want here...
- Parameters:
conn
- The WebSocket instance this event is occurring on.message
- The UTF-8 decoded message that was received.
-
onWebsocketMessage
final void onWebsocketMessage(WebSocket conn, ByteBuffer blob)
Called when an entire binary frame has been received. Do whatever you want here...
- Parameters:
conn
- The WebSocket instance this event is occurring on.blob
- The binary message that was received.
-
onWebsocketOpen
final void onWebsocketOpen(WebSocket conn, Handshakedata handshake)
Called after onHandshakeReceived returns true. Indicates that a completeWebSocket connection has been established, and we are ready to send/receive data.
- Parameters:
conn
- The WebSocket instance this event is occurring on.
-
onWebsocketClose
final void onWebsocketClose(WebSocket conn, int code, String reason, boolean remote)
Called after WebSocket#close is explicity called, or when the other end of theWebSocket connection is closed.
- Parameters:
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 remotehost.
-
onWebsocketError
final void onWebsocketError(WebSocket conn, Exception ex)
Called if an exception worth noting occurred. If an error causes the connection to fail onClosewill be called additionally afterwards.
- Parameters:
conn
- The WebSocket instance this event is occurring on.ex
- The exception that occurred.
-
onWriteDemand
final void onWriteDemand(WebSocket w)
This method is used to inform the selector thread that there is data queued to be written tothe socket.
-
onWebsocketCloseInitiated
void onWebsocketCloseInitiated(WebSocket conn, int code, String reason)
send when this peer sends a close handshake
- Parameters:
code
- The codes can be looked up here: CloseFramereason
- Additional information string
-
onWebsocketClosing
void onWebsocketClosing(WebSocket conn, int code, String reason, boolean remote)
Called as soon as no further frames are accepted
- Parameters:
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 remotehost.
-
onCloseInitiated
void onCloseInitiated(WebSocket conn, int code, String reason)
-
setWebSocketFactory
final void setWebSocketFactory(WebSocketServerFactory wsf)
-
getWebSocketFactory
final WebSocketFactory getWebSocketFactory()
-
getLocalSocketAddress
InetSocketAddress getLocalSocketAddress(WebSocket conn)
- Parameters:
conn
- The WebSocket instance this event is occurring on.
-
getRemoteSocketAddress
InetSocketAddress getRemoteSocketAddress(WebSocket conn)
- Parameters:
conn
- The WebSocket instance this event is occurring on.
-
onOpen
abstract void onOpen(WebSocket conn, ClientHandshake handshake)
Called after an opening handshake has been performed and the given websocket is ready to bewritten on.
- Parameters:
conn
- The WebSocket instance this event is occurring on.handshake
- The handshake of the websocket instance
-
onClose
abstract void onClose(WebSocket conn, int code, String reason, boolean remote)
Called after the websocket connection has been closed.
- Parameters:
conn
- The WebSocket instance this event is occurring 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 remotehost.
-
onMessage
abstract void onMessage(WebSocket conn, String message)
Callback for string messages received from the remote host
- Parameters:
conn
- The WebSocket instance this event is occurring on.message
- The UTF-8 decoded message that was received.
-
onError
abstract void onError(WebSocket conn, Exception ex)
Called when errors occurs. If an error causes the websocket connection to fail will be called additionally. This method will becalled primarily because of IO or protocol errors. If the given exception is anRuntimeException that probably means that you encountered a bug.
- Parameters:
conn
- Can be null if there error does not belong to one specific websocket.ex
- The exception causing this error
-
onStart
abstract void onStart()
Called when the server started up successfully.
If any error occurred, onError is called instead.
-
onMessage
void onMessage(WebSocket conn, ByteBuffer message)
Callback for binary messages received from the remote host
- Parameters:
conn
- The WebSocket instance this event is occurring on.message
- The binary message that was received.
-
broadcast
void broadcast(String text)
Send a text to all connected endpoints
- Parameters:
text
- the text to send to the endpoints
-
broadcast
void broadcast(Array<byte> data)
Send a byte array to all connected endpoints
- Parameters:
data
- the data to send to the endpoints
-
broadcast
void broadcast(ByteBuffer data)
Send a ByteBuffer to all connected endpoints
- Parameters:
data
- the data to send to the endpoints
-
broadcast
void broadcast(Array<byte> data, Collection<WebSocket> clients)
Send a byte array to a specific collection of websocket connections
- Parameters:
data
- the data to send to the endpointsclients
- a collection of endpoints to whom the text has to be send
-
broadcast
void broadcast(ByteBuffer data, Collection<WebSocket> clients)
Send a ByteBuffer to a specific collection of websocket connections
- Parameters:
data
- the data to send to the endpointsclients
- a collection of endpoints to whom the text has to be send
-
broadcast
void broadcast(String text, Collection<WebSocket> clients)
Send a text to a specific collection of websocket connections
- Parameters:
text
- the text to send to the endpointsclients
- a collection of endpoints to whom the text has to be send
-
-
-
-