Class WebSocket

java.lang.Object
rosequartz.net.WebSocket

public class WebSocket extends Object
Represents a socket using the WebSocket protocol. Sockets are used to communicate between computers. This type of socket is supported on all build targets.
  • Constructor Details

    • WebSocket

      public WebSocket()
      Creates a new WebSocket.
  • Method Details

    • connect

      public WebSocket connect(String url)
      Launches a new thread and connects this socket to another socket given its full url.
      Parameters:
      url - the full url of the socket to connect to
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • connectBlocking

      public WebSocket connectBlocking(String url)
      Connects this socket to another socket given its full url, blocking this thread until a connection is established.
      Parameters:
      url - the full url of the socket to connect to
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
    • accept

      public WebSocket accept(int port)
      Launches a new thread and accepts one connection from any socket on the given port. To handle multiple connections to sockets from the same port, create a new socket with the same port.

      WARNING! WebSocket server-behavior is not supported by the browser runtime. The browser-runtime will throw a 'PlatformSpecificUnsupportedException'.
      Parameters:
      port - port to listen on
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
      PlatformSpecificUnsupportedException - if called in browser build target
    • acceptBlocking

      public WebSocket acceptBlocking(int port)
      Accepts one connection from any socket on the given port. To handle multiple connections to sockets from the same port, create a new socket with the same port.

      WARNING! WebSocket server-behavior is not supported by the browser runtime. The browser-runtime will throw a 'PlatformSpecificUnsupportedException'.
      Parameters:
      port - port to listen on
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is not closed
      PlatformSpecificUnsupportedException - if called in browser build target
    • onConnect

      public final WebSocket 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 WebSocket onMessage(Consumer<String> 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(String 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 WebSocket 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 WebSocket send(String 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 WebSocket close()
      Closes the socket, ending its current connection.
      Returns:
      this
      Throws:
      IllegalStateException - if the socket is closed
    • getState

      public WebSocket.State getState()
      Gets the current state of the socket.
      Returns:
      state