Package rosequartz.net
Class UDPSocket<T extends Serializable>
java.lang.Object
rosequartz.net.UDPSocket<T>
- Type Parameters:
T- the type of data to send. To send pure string data, use the String-type.
Represents a socket using the UDP protocol.
Sockets are used to communicate between computers.
The UDP protocol does not guarantee that all messages reach their destination, but has lower latency than TCP.
WARNING! UDPSocket is not supported by the browser runtime. Only use this class if you do NOT plan on releasing your project for browsers.
WARNING! UDPSocket is not supported by the browser runtime. Only use this class if you do NOT plan on releasing your project for browsers.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFunctional interface for message handler.static classRepresents the current state of the socket. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclose()Closes the socket.Gets the address of this socket.intgetPort()Gets the current port of the socket.getState()Gets the current state of the socket.protected voidMethod called when the socket is closed.Set a function to be called when the socket is closed.onMessage(UDPSocket.OnMessageFunction<T> action)Set a function to be called when a message is received.protected voidMethod called when a message is received.open()Opens the socket.Sends a message to the connected socket.setBufferSize(int bufferSize)Sets the buffer size of the socket.setPort(int port)Sets the port of the socket.
-
Constructor Details
-
UDPSocket
Creates a new UDP-Socket from a buffer size on a random port.- Parameters:
bufferSize- buffer size- Throws:
PlatformSpecificDisabledException- if not enabled
-
UDPSocket
Creates a new UDP-Socket from a buffer size and a port.- Parameters:
port- portbufferSize- buffer size- Throws:
PlatformSpecificDisabledException- if not enabled
-
-
Method Details
-
setPort
Sets the port of the socket.- Parameters:
port- port- Returns:
- this
- Throws:
IllegalStateException- if the socket is not closed
-
setBufferSize
Sets the buffer size of the socket.- Parameters:
bufferSize- buffer size- Returns:
- this
- Throws:
IllegalStateException- if the socket is not closed
-
open
Opens the socket.- Returns:
- this
- Throws:
IllegalStateException- if the socket is not closed
-
onMessage
Set a function to be called when a message is received.- Parameters:
action- function to call on message receive- Returns:
- this
-
onMessage
Method called when a message is received. By default, this method calls the configured function by 'onMessage(OnMessageFunctionaction)', but it may be overridden. -
onClose
Set a function to be called when the socket is closed.- Parameters:
action- function to call on socket close- Returns:
- this
-
onClose
Method called when the socket is closed. By default, this method calls the configured function by 'onClose(Consumeraction)', but it may be overridden. -
send
Sends a message to the connected socket.- Parameters:
message- message to sendaddress- address of the receiving socketport- port of the receiving socket- Returns:
- this
- Throws:
IllegalStateException- if the socket is not open
-
close
Closes the socket.- Returns:
- this
- Throws:
IllegalStateException- if the socket is not open
-
getState
Gets the current state of the socket.- Returns:
- state
-
getPort
public int getPort()Gets the current port of the socket.- Returns:
- port
-
getAddress
Gets the address of this socket.
-