Class NetworkClient

java.lang.Object
dev.adlin.vts4j.network.NetworkClient

public class NetworkClient extends Object
High-level network client that delegates infrastructure operations to an underlying socket client.

Implements both asynchronous and synchronous (blocking) connection lifecycle management methods, alongside payload dispatching and custom event hook registrations.

  • Constructor Summary

    Constructors
    Constructor
    Description
    NetworkClient(@NonNull URI address)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Establishes a synchronous connection to the remote server, blocking the calling thread indefinitely until the handshake completes or fails.
    void
    awaitConnect(long timeout, @NonNull TimeUnit timeUnit)
    Establishes a synchronous connection to the remote server, blocking the calling thread up to the specified timeout interval.
    void
    Synchronously tears down the active socket session, blocking the calling thread until the close handshake is acknowledged.
    void
    Initiates a non-blocking background connection routine to the remote server.
    void
    Initiates an asynchronous non-blocking disconnection routine.
    void
    send(@NonNull String payload)
    Transmits a raw string payload over the active socket session.
    void
    setCloseHandler(@NonNull Consumer<CloseReason> closeReason)
    Assigns a callback hook triggered immediately after the socket session terminates.
    void
    setErrorHandler(@NonNull Consumer<Exception> handler)
    Assigns a custom error monitoring callback hook.
    void
    setMessageHandler(@NonNull Consumer<String> handleMessage)
    Assigns the main core callback interceptor for incoming data packets.
    void
    setOpenHandler(@NonNull Consumer<org.java_websocket.handshake.ServerHandshake> handshake)
    Assigns a callback hook triggered upon successful completion of the opening handshake.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NetworkClient

      public NetworkClient(@NonNull URI address)
  • Method Details

    • awaitConnect

      public void awaitConnect()
      Establishes a synchronous connection to the remote server, blocking the calling thread indefinitely until the handshake completes or fails.
      Throws:
      RuntimeException - if the thread is interrupted while waiting to connect
    • awaitConnect

      public void awaitConnect(long timeout, @NonNull TimeUnit timeUnit)
      Establishes a synchronous connection to the remote server, blocking the calling thread up to the specified timeout interval.
      Parameters:
      timeout - the maximum duration to wait for a connection
      timeUnit - the time unit of the timeout argument
      Throws:
      RuntimeException - if the thread is interrupted while waiting to connect
    • connect

      public void connect()
      Initiates a non-blocking background connection routine to the remote server.
    • awaitDisconnect

      public void awaitDisconnect()
      Synchronously tears down the active socket session, blocking the calling thread until the close handshake is acknowledged.
      Throws:
      RuntimeException - if the thread is interrupted while executing the close procedure
    • disconnect

      public void disconnect()
      Initiates an asynchronous non-blocking disconnection routine.
    • send

      public void send(@NonNull String payload)
      Transmits a raw string payload over the active socket session.
      Parameters:
      payload - the message payload string to send to the server
    • setMessageHandler

      public void setMessageHandler(@NonNull Consumer<String> handleMessage)
      Assigns the main core callback interceptor for incoming data packets.
      Parameters:
      handleMessage - functional consumer handling inbound raw strings
    • setErrorHandler

      public void setErrorHandler(@NonNull Consumer<Exception> handler)
      Assigns a custom error monitoring callback hook.
      Parameters:
      handler - functional consumer handling raw network or protocol exceptions
    • setOpenHandler

      public void setOpenHandler(@NonNull Consumer<org.java_websocket.handshake.ServerHandshake> handshake)
      Assigns a callback hook triggered upon successful completion of the opening handshake.
      Parameters:
      handshake - functional consumer processing handshake details
    • setCloseHandler

      public void setCloseHandler(@NonNull Consumer<CloseReason> closeReason)
      Assigns a callback hook triggered immediately after the socket session terminates.
      Parameters:
      closeReason - functional consumer analyzing closure state parameters