Class TCPSocket<T extends Serializable>

java.lang.Object
rosequartz.net.TCPSocket<T>
Type Parameters:
T - the type of data to send. To send pure string data, use the String-type.

public class TCPSocket<T extends Serializable> extends Object
Represents a socket using the TCP protocol. Sockets are used to communicate between computers. The TCP protocol guarantees that all messages reach their destination.

WARNING! TCPSocket 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 TCPSocket<T> setPort(int port)
      Sets the listener port of the socket.
      Parameters:
      port - listener port
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • connect

      public TCPSocket<T> connect(String address, int port)
      Launches a new thread and connects this socket to another socket given its address and port.
      Parameters:
      address - the address of the socket to connect to
      port - the port of the socket to connect to
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • connectBlocking

      public TCPSocket<T> connectBlocking(String address, int port)
      Connects this socket to another socket given its address and port, blocking this thread until a connection is established.
      Parameters:
      address - the address of the socket to connect to
      port - the port of the socket to connect to
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • accept

      public TCPSocket<T> accept()
      Launches a new thread and accepts one connection from any socket on the configured listener port. To handle multiple connections to sockets from the same port, create a new socket with the same port.
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • acceptBlocking

      public TCPSocket<T> acceptBlocking()
      Accepts one connection from any socket on the configured listener port, blocking this thread until a connection is established. To handle multiple connections to sockets from the same port, create a new socket with the same port.
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • onConnect

      public final TCPSocket<T> onConnect(Runnable action)
      Set a function to be called when the socket is connected.
      Parameters:
      action - function to call on socket connection
      Returns:
      this
    • onConnect

      protected void onConnect()
      Method called when a connection is established. By default, this method calls the configured function by 'onConnect(Runnable action)', but it may be overridden.
    • onMessage

      public final TCPSocket<T> onMessage(Consumer<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)
      Method called when a message is received. By default, this method calls the configured function by 'onMessage(Consumer action)', but it may be overridden.
    • onClose

      public final TCPSocket<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 TCPSocket<T> send(T message)
      Sends a message to the connected socket.
      Parameters:
      message - message to send
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not connected
    • close

      public TCPSocket<T> close()
      Closes the socket, ending its current connection.
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is closed
    • getState

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

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

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

      public int getConnectedPort()
      Gets the port of the socket this socket is connected to.
      Throws:
      IllegalStateException - if the socket is not connected
    • getConnectedAddress

      public String getConnectedAddress()
      Gets the address of the socket this socket is connected to.
      Throws:
      IllegalStateException - if the socket is not connected