Class WebSocketServer
java.lang.Object
de.craftsblock.craftsnet.api.Server
de.craftsblock.craftsnet.api.websocket.WebSocketServer
The WebSocketServer class represents a simple WebSocket server implementation. It allows WebSocket clients to connect,
manages their connections, and enables sending messages to connected clients.
The server can be configured to use SSL encryption by providing the necessary SSL key file.
It uses a ServerSocket to listen for incoming connections on the specified port.
- Since:
- 2.1.1-SNAPSHOT
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionWebSocketServer(CraftsNet craftsNet, int port, boolean ssl) Constructs a WebSocketServer instance with the specified port number.WebSocketServer(CraftsNet craftsNet, int port, int backlog, boolean ssl) Constructs a WebSocketServer instance with the specified port number and backlog size. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidadd(String path, WebSocketClient client) Adds a WebSocket client to a specified path.voidPerform necessary actions when the server is awakened or warn if needed.voidbind(int port, int backlog) Binds the server to the specified port and backlog.voidSends a message to all connected WebSocket clients.voidSends a message to all connected WebSocket clients with a specified path.Retrieves the list of all currently connectedWebSocketClients.intReturns the size which should every fragment of a frame should have.Retrieves theTypeEncoderRegistryinstance managing the registration and lookup ofWebSocketSafeTypeEncodercodecs.booleanChecks if the server is enabled.booleanisSSL()Checks if SSL is enabled for the server.protected voidremove(WebSocketClient client) Removes a WebSocket client from the server and the associated path.voidsetFragmentationEnabled(boolean shouldFragment) Enable or disable fragmentation of messages send by the server.voidsetFragmentSize(int fragmentSize) Sets the maximum size of each fragment of a frame.booleanReturns whether fragmentation is enabled or not.booleanChecks if SSL should be enabled for the server.voidPuts the server to sleep if it is not needed.voidstart()Starts the server.voidstop()Stops the server.Methods inherited from class de.craftsblock.craftsnet.api.Server
bind, getBacklog, getCraftsNet, getPort, isRunning, shouldUseSSL
-
Constructor Details
-
WebSocketServer
Constructs a WebSocketServer instance with the specified port number.- Parameters:
craftsNet- The CraftsNet instance which instantiates this websocket server.port- The port number on which the server will listen for incoming connections.ssl- A boolean flag indicating whether SSL encryption should be used (true for HTTPS, false for HTTP).
-
WebSocketServer
Constructs a WebSocketServer instance with the specified port number and backlog size.- Parameters:
craftsNet- The CraftsNet instance which instantiates this websocket server.port- The port number on which the server will listen for incoming connections.backlog- The size of the backlog for the server socket.ssl- A boolean flag indicating whether SSL encryption should be used.
-
-
Method Details
-
bind
public void bind(int port, int backlog) Binds the server to the specified port and backlog. -
start
public void start()Starts the server. -
stop
public void stop()Stops the server. -
awakeOrWarn
public void awakeOrWarn()Perform necessary actions when the server is awakened or warn if needed.- Specified by:
awakeOrWarnin classServer
-
sleepIfNotNeeded
public void sleepIfNotNeeded()Puts the server to sleep if it is not needed.- Specified by:
sleepIfNotNeededin classServer
-
isEnabled
public boolean isEnabled()Checks if the server is enabled. -
isSSL
public boolean isSSL()Checks if SSL is enabled for the server. -
shouldUseSSL
public boolean shouldUseSSL()Checks if SSL should be enabled for the server.- Returns:
trueif SSL should be enabled,falseotherwise.
-
getTypeEncoderRegistry
Retrieves theTypeEncoderRegistryinstance managing the registration and lookup ofWebSocketSafeTypeEncodercodecs.- Returns:
- the
TypeEncoderRegistryforWebSocketSafeTypeEncoderinstances - Since:
- 3.5.0
-
shouldFragment
@Experimental public boolean shouldFragment()Returns whether fragmentation is enabled or not.- Returns:
- true if fragmentation is enabled, false otherwise
-
setFragmentationEnabled
@Experimental public void setFragmentationEnabled(boolean shouldFragment) Enable or disable fragmentation of messages send by the server.- Parameters:
shouldFragment- true if fragmentation should be enabled, false otherwise.
-
getFragmentSize
@Experimental public int getFragmentSize()Returns the size which should every fragment of a frame should have.- Returns:
- The max size of each frame.
-
setFragmentSize
@Experimental public void setFragmentSize(int fragmentSize) Sets the maximum size of each fragment of a frame.- Parameters:
fragmentSize- The max size of the fragments.
-
broadcast
Sends a message to all connected WebSocket clients.- Parameters:
data- The message to be sent.
-
broadcast
Sends a message to all connected WebSocket clients with a specified path.- Parameters:
path- The path to which the clients are assigned.data- The message to be sent.
-
add
Adds a WebSocket client to a specified path.- Parameters:
path- The path to which the client will be assigned.client- The WebSocket client that will be added.
-
remove
Removes a WebSocket client from the server and the associated path.- Parameters:
client- The WebSocket client that will be removed.
-
getClients
Retrieves the list of all currently connectedWebSocketClients.- Returns:
- The list of the connected
WebSocketClients.
-