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.

public class UDPSocket<T extends Serializable> extends Object
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.
  • Constructor Details

  • Method Details

    • setPort

      public UDPSocket<T> setPort(int port)
      Sets the port of the socket.
      Parameters:
      port - port
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • setBufferSize

      public UDPSocket<T> setBufferSize(int bufferSize)
      Sets the buffer size of the socket.
      Parameters:
      bufferSize - buffer size
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • open

      public UDPSocket<T> open()
      Opens the socket.
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • onMessage

      public final UDPSocket<T> onMessage(UDPSocket.OnMessageFunction<T> action)
      Set a function to be called when a message is received.
      Parameters:
      action - function to call on message receive
      Returns:
      this
    • onMessage

      protected void onMessage(T message, String address, int port)
      Method called when a message is received. By default, this method calls the configured function by 'onMessage(OnMessageFunction action)', but it may be overridden.
    • onClose

      public final UDPSocket<T> onClose(Consumer<Throwable> action)
      Set a function to be called when the socket is closed.
      Parameters:
      action - function to call on socket close
      Returns:
      this
    • onClose

      protected void onClose(Throwable reason)
      Method called when the socket is closed. By default, this method calls the configured function by 'onClose(Consumer action)', but it may be overridden.
    • send

      public UDPSocket<T> send(T message, String address, int port)
      Sends a message to the connected socket.
      Parameters:
      message - message to send
      address - address of the receiving socket
      port - port of the receiving socket
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not open
    • close

      public UDPSocket<T> close()
      Closes the socket.
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not open
    • getState

      public UDPSocket.State getState()
      Gets the current state of the socket.
      Returns:
      state
    • getPort

      public int getPort()
      Gets the current port of the socket.
      Returns:
      port
    • getAddress

      public String getAddress()
      Gets the address of this socket.