Class WebSocketClient
java.lang.Object
de.craftsblock.craftsnet.api.websocket.WebSocketClient
- All Implemented Interfaces:
RequireAble,Runnable
The WebSocketClient class represents a WebSocket client that connects to the WebSocketServer.
When a WebSocket client connects, the server creates a new instance of this class to manage the client's connection. The client sends a handshake request, and if the server validates it, the client is assigned to a specific endpoint based on the requested path. The client continuously listens for incoming messages and invokes the appropriate endpoint's method to process the received data.
The class also handles the disconnection of the client, sending messages, and reading headers and messages from the socket.
- Since:
- 2.1.1-SNAPSHOT
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionWebSocketClient(CraftsNet craftsNet, Socket socket, WebSocketServer server) Creates a new WebSocketClient with the provided socket and server. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Disconnects the client gracefully without providing any information about the reason.voidDisconnects the client gracefully with a specified code and reason.voidclose(ClosureCode code, String reason) Disconnects the client gracefully with a specified pre-defined code and reason.voidDisconnects the client gracefully with a specified reason.protected voidDisconnects the WebSocket client and performs necessary cleanup operations.Returns the domain used by the connected client.Retrieves the registered endpoint corresponding to the given header.intReturns the size which should every fragment of a frame should have.Retrieves the value of the given header key from the client's headers.Returns a List containing all the headers received from the client.getIp()Returns the IP address of the connected client.getPath()Returns the requested path from the client's headers.Returns the storage of this websocket client for storing specific data.booleanisActive()Returns whether the websocket runnable has been started.booleanReturns whether the web socket is still connected or the connection has been closed / failed.voidrun()Runs the WebSocket client to handle incoming messages and manage connections.voidsendMessage(byte[] data) Sends a message to the connected WebSocket client.voidsendMessage(de.craftsblock.craftscore.json.Json data) Sends a message to the connected WebSocket client.voidsendMessage(ByteBuffer data) Sends a message to the connected WebSocket client.voidsendMessage(Object data) Sends a message to the connected WebSocket client.voidsendMessage(String data) Sends a message to the connected WebSocket client.voidsendPing()Sends a ping to the connected WebSocket client.voidsendPing(byte[] message) Sends a ping with the provided payload to the connected WebSocket client.voidSends a ping with the provided payload to the connected WebSocket client.voidsendPong()Sends a pong to the connected WebSocket client.voidsendPong(byte[] message) Sends a pong with the provided payload to the connected WebSocket client.voidSends a pong with the provided payload to the connected WebSocket client.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.voidsetMaskingOutgoing(boolean shouldMaskOutgoing) Sets whether the outgoing message should be masked or not.booleanReturns whether fragmentation is enabled or not.booleanReturns whether the outgoing messages should be masked or not.
-
Constructor Details
-
WebSocketClient
Creates a new WebSocketClient with the provided socket and server.- Parameters:
craftsNet- The CraftsNet instance which instantiates thissocket- The Socket used for communication with the client.server- The WebSocketServer to which this client belongs.
-
-
Method Details
-
run
public void run()Runs the WebSocket client to handle incoming messages and manage connections. This method reads the client's headers, sends a handshake to establish the WebSocket connection, and then processes incoming messages from the client using a registered endpoint. -
getEndpoint
@Nullable public @Nullable EnumMap<ProcessPriority.Priority,List<RouteRegistry.EndpointMapping>> getEndpoint()Retrieves the registered endpoint corresponding to the given header.- Returns:
- The corresponding list of SocketMappings if found, or null if not found.
-
getPath
Returns the requested path from the client's headers.- Returns:
- The requested path as a String.
-
getHeader
Retrieves the value of the given header key from the client's headers.- Parameters:
key- The header key for which the value is requested.- Returns:
- The value of the header if found, or null if the header is not present.
-
getIp
Returns the IP address of the connected client.- Returns:
- The IP address as a String.
-
getDomain
Returns the domain used by the connected client.- Returns:
- The domain as a String.
-
getHeaders
Returns a List containing all the headers received from the client.- Returns:
- A List of Strings representing the headers from the client.
-
getSession
Returns the storage of this websocket client for storing specific data.- Returns:
- The storage of this websocket client instance.
-
shouldMaskOutgoing
public boolean shouldMaskOutgoing()Returns whether the outgoing messages should be masked or not.- Returns:
trueif the outgoing messages should be masked,falseotherwise.
-
setMaskingOutgoing
public void setMaskingOutgoing(boolean shouldMaskOutgoing) Sets whether the outgoing message should be masked or not.- Parameters:
shouldMaskOutgoing-trueif the outgoing messages should be masked,falseotherwise.
-
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.
-
sendMessage
public void sendMessage(de.craftsblock.craftscore.json.Json data) Sends a message to the connected WebSocket client.- Parameters:
data- The message to be sent, as it's json representation.
-
sendMessage
Sends a message to the connected WebSocket client.- Parameters:
data- The message to be sent, as it's string representation.
-
sendMessage
Sends a message to the connected WebSocket client.- Parameters:
data- The message to be sent, as a bytebuffer.
-
sendMessage
public void sendMessage(byte[] data) Sends a message to the connected WebSocket client.- Parameters:
data- The message to be sent, as an array of bytes.
-
sendMessage
Sends a message to the connected WebSocket client.This method will try to parse the object in the following order:
Stringbyte[]JsonByteBuffer
Object.toString()and then sent.- Parameters:
data- The message to be sent, as an object.- Since:
- 3.4.3
-
sendPing
public void sendPing()Sends a ping to the connected WebSocket client. -
sendPing
Sends a ping with the provided payload to the connected WebSocket client.- Parameters:
message- The payload as a string which should be appended
-
sendPing
public void sendPing(byte[] message) Sends a ping with the provided payload to the connected WebSocket client.- Parameters:
message- The payload as a byte array which should be appended
-
sendPong
public void sendPong()Sends a pong to the connected WebSocket client. -
sendPong
Sends a pong with the provided payload to the connected WebSocket client.- Parameters:
message- The payload as a string which should be appended
-
sendPong
public void sendPong(byte[] message) Sends a pong with the provided payload to the connected WebSocket client.- Parameters:
message- The payload as a byte array which should be appended
-
close
public void close()Disconnects the client gracefully without providing any information about the reason. -
close
Disconnects the client gracefully with a specified reason.- Parameters:
reason- The reason why the client has been closed.- Throws:
IllegalStateException- If the code used to close the connection is only for internal use.
-
close
Disconnects the client gracefully with a specified pre-defined code and reason.- Parameters:
code- The pre-defined code that is responsible for closing the socket.reason- The reason why the client has been closed.- Throws:
IllegalStateException- If the code used to close the connection is only for internal use.
-
close
Disconnects the client gracefully with a specified code and reason.- Parameters:
code- The close code.reason- The reason why the client has been closed.- Throws:
IllegalStateException- If the code used to close the connection is only for internal use.
-
isActive
public boolean isActive()Returns whether the websocket runnable has been started.- Returns:
- True if the websocket runnable was started, false otherwise
-
isConnected
public boolean isConnected()Returns whether the web socket is still connected or the connection has been closed / failed.- Returns:
- True if the websocket is connected, false otherwise or if the connection failed
-
disconnect
protected void disconnect()Disconnects the WebSocket client and performs necessary cleanup operations. This method triggers the ClientDisconnectEvent before closing the socket and removing the client from the server.
-