public abstract class WebSocketServer extends AbstractWebSocket implements java.lang.Runnable
限定符和类型 | 类和说明 |
---|---|
static interface |
WebSocketServer.WebSocketServerFactory
Interface to encapsulate the required methods for a websocket factory
|
class |
WebSocketServer.WebSocketWorker
This class is used to process incoming data
|
限定符和类型 | 字段和说明 |
---|---|
static int |
DECODERS |
构造器和说明 |
---|
WebSocketServer()
Creates a WebSocketServer that will attempt to
listen on port WebSocket.DEFAULT_PORT.
|
WebSocketServer(java.net.InetSocketAddress address)
Creates a WebSocketServer that will attempt to bind/listen on the given address.
|
WebSocketServer(java.net.InetSocketAddress address,
int decodercount) |
WebSocketServer(java.net.InetSocketAddress address,
int decodercount,
java.util.List<Draft> drafts) |
WebSocketServer(java.net.InetSocketAddress address,
int decodercount,
java.util.List<Draft> drafts,
java.util.Collection<WebSocket> connectionscontainer)
Creates a WebSocketServer that will attempt to bind/listen on the given address,
and comply with Draft version draft.
|
WebSocketServer(java.net.InetSocketAddress address,
java.util.List<Draft> drafts) |
限定符和类型 | 方法和说明 |
---|---|
java.util.Collection<WebSocket> |
connections()
Returns a WebSocket[] of currently connected clients.
|
java.nio.ByteBuffer |
createBuffer() |
java.net.InetSocketAddress |
getAddress() |
java.util.List<Draft> |
getDraft() |
java.net.InetSocketAddress |
getLocalSocketAddress(WebSocket conn) |
int |
getPort()
Gets the port number that this server listens on.
|
java.net.InetSocketAddress |
getRemoteSocketAddress(WebSocket conn) |
WebSocketFactory |
getWebSocketFactory() |
abstract void |
onClose(WebSocket conn,
int code,
java.lang.String reason,
boolean remote)
Called after the websocket connection has been closed.
|
void |
onCloseInitiated(WebSocket conn,
int code,
java.lang.String reason) |
void |
onClosing(WebSocket conn,
int code,
java.lang.String reason,
boolean remote) |
abstract void |
onError(WebSocket conn,
java.lang.Exception ex)
Called when errors occurs.
|
void |
onFragment(WebSocket conn,
Framedata fragment)
Callback for fragmented frames
|
void |
onMessage(WebSocket conn,
java.nio.ByteBuffer message)
Callback for binary messages received from the remote host
|
abstract void |
onMessage(WebSocket conn,
java.lang.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,
java.lang.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,
java.lang.String reason)
send when this peer sends a close handshake
|
void |
onWebsocketClosing(WebSocket conn,
int code,
java.lang.String reason,
boolean remote)
Called as soon as no further frames are accepted
|
void |
onWebsocketError(WebSocket conn,
java.lang.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,
java.nio.ByteBuffer blob)
Called when an entire binary frame has been received.
|
void |
onWebsocketMessage(WebSocket conn,
java.lang.String message)
Called when an entire text frame has been received.
|
void |
onWebsocketMessageFragment(WebSocket conn,
Framedata frame)
已过时。
|
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.
|
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.
|
getConnectionLostTimeout, isTcpNoDelay, setConnectionLostTimeout, setTcpNoDelay
getFlashPolicy, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPong
public WebSocketServer()
more details here
public WebSocketServer(java.net.InetSocketAddress address)
address
- The address to listen tomore details here
public WebSocketServer(java.net.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(java.net.InetSocketAddress address, java.util.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(java.net.InetSocketAddress address, int decodercount, java.util.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(java.net.InetSocketAddress address, int decodercount, java.util.List<Draft> drafts, java.util.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.java.lang.IllegalStateException
- Starting an instance againpublic void stop(int timeout) throws java.lang.InterruptedException
timeout
- Specifies how many milliseconds the overall close handshaking may take altogether before the connections are closed without proper close handshaking.java.lang.InterruptedException
- Interruptpublic void stop() throws java.io.IOException, java.lang.InterruptedException
java.io.IOException
java.lang.InterruptedException
public java.util.Collection<WebSocket> connections()
public java.net.InetSocketAddress getAddress()
public int getPort()
public java.util.List<Draft> getDraft()
public void run()
run
在接口中 java.lang.Runnable
public java.nio.ByteBuffer createBuffer()
public final void onWebsocketMessage(WebSocket conn, java.lang.String message)
WebSocketListener
onWebsocketMessage
在接口中 WebSocketListener
conn
- 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)
WebSocketAdapter
onWebsocketMessageFragment
在接口中 WebSocketListener
onWebsocketMessageFragment
在类中 WebSocketAdapter
conn
- The WebSocket instance this event is occurring on.frame
- The fragmented frameWebSocketListener.onWebsocketMessageFragment(WebSocket, Framedata)
public final void onWebsocketMessage(WebSocket conn, java.nio.ByteBuffer blob)
WebSocketListener
onWebsocketMessage
在接口中 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
在接口中 WebSocketListener
conn
- The WebSocket instance this event is occuring on.handshake
- The handshake of the websocket instancepublic final void onWebsocketClose(WebSocket conn, int code, java.lang.String reason, boolean remote)
WebSocketListener
onWebsocketClose
在接口中 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 ServerHandshakeBuilder onWebsocketHandshakeReceivedAsServer(WebSocket conn, Draft draft, ClientHandshake request) throws InvalidDataException
WebSocketAdapter
onWebsocketHandshakeReceivedAsServer
在接口中 WebSocketListener
onWebsocketHandshakeReceivedAsServer
在类中 WebSocketAdapter
conn
- 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)
public final void onWebsocketError(WebSocket conn, java.lang.Exception ex)
WebSocketListener
onWebsocketError
在接口中 WebSocketListener
conn
- The WebSocket instance this event is occuring on.ex
- The exception that occurred. public final void onWriteDemand(WebSocket w)
WebSocketListener
onWriteDemand
在接口中 WebSocketListener
w
- The WebSocket instance this event is occuring on.public void onWebsocketCloseInitiated(WebSocket conn, int code, java.lang.String reason)
WebSocketListener
onWebsocketCloseInitiated
在接口中 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, java.lang.String reason, boolean remote)
WebSocketListener
onWebsocketClosing
在接口中 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 void onCloseInitiated(WebSocket conn, int code, java.lang.String reason)
public void onClosing(WebSocket conn, int code, java.lang.String reason, boolean remote)
public final void setWebSocketFactory(WebSocketServer.WebSocketServerFactory wsf)
public final WebSocketFactory getWebSocketFactory()
public java.net.InetSocketAddress getLocalSocketAddress(WebSocket conn)
getLocalSocketAddress
在接口中 WebSocketListener
conn
- The WebSocket instance this event is occuring on.WebSocket.getLocalSocketAddress()
public java.net.InetSocketAddress getRemoteSocketAddress(WebSocket conn)
getRemoteSocketAddress
在接口中 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, java.lang.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, java.lang.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, java.lang.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, java.nio.ByteBuffer message)
conn
- The WebSocket instance this event is occurring on.message
- The binary message that was received.onMessage(WebSocket, ByteBuffer)
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)