Module io.jooby
Package io.jooby

Interface WebSocket


public interface WebSocket
Websocket. Usage:

 ws("/pattern", (ctx, configurer) -> {
   configurer.onConnect(ws -> {
     // Connect callback
   }):

   configurer.onMessage((ws, message) -> {
     ws.send("Got: " + message.value());
   });

   configurer.onClose((ws, closeStatus) -> {
     // Closing websocket
   });

   configurer.onError((ws, cause) -> {

   });
 });

 
Since:
2.2.0
Author:
edgar
  • Field Details

    • MAX_BUFFER_SIZE

      static final int MAX_BUFFER_SIZE
      Max message size for websocket (128K).
      See Also:
  • Method Details

    • getContext

      @NonNull Context getContext()
      Originating HTTP context. Please note this is a read-only context, so you are not allowed to modify or produces a response from it.

      The context let give you access to originating request (then one that was upgrade it).

      Returns:
      Read-only originating HTTP request.
    • getAttributes

      @NonNull default Map<String,Object> getAttributes()
      Context attributes (a.k.a request attributes).
      Returns:
      Context attributes.
    • attribute

      @NonNull default <T> T attribute(@NonNull String key)
      Get an attribute by his key. This is just an utility method around getAttributes(). This method look first in current context and fallback to application attributes.
      Type Parameters:
      T - Attribute type.
      Parameters:
      key - Attribute key.
      Returns:
      Attribute value.
    • attribute

      @NonNull default WebSocket attribute(@NonNull String key, Object value)
      Set an application attribute.
      Parameters:
      key - Attribute key.
      value - Attribute value.
      Returns:
      This router.
    • getSessions

      @NonNull List<WebSocket> getSessions()
      Web sockets connected to the same path. This method doesn't include the current websocket.
      Returns:
      Web sockets or empty list.
    • isOpen

      boolean isOpen()
      True if websocket is open.
      Returns:
      True when open.
    • forEach

      void forEach(SneakyThrows.Consumer<WebSocket> callback)
      For each of live sessions (including this) do something with it.
      
       Broadcast example:
      
       ws.forEach(session -> {
         session.send("Message");
       });
      
       
      Parameters:
      callback - Callback.
    • send

      @NonNull default WebSocket send(@NonNull String message)
      Send a text message to client.
      Parameters:
      message - Text Message.
      Returns:
      This websocket.
    • send

      @NonNull WebSocket send(@NonNull String message, @NonNull WebSocket.WriteCallback callback)
      Send a text message to client.
      Parameters:
      message - Text Message.
      callback - Write callback.
      Returns:
      This websocket.
    • send

      @NonNull default WebSocket send(@NonNull byte[] message)
      Send a text message to client.
      Parameters:
      message - Text Message.
      Returns:
      This websocket.
    • send

      @NonNull WebSocket send(@NonNull byte[] message, @NonNull WebSocket.WriteCallback callback)
      Send a text message to client.
      Parameters:
      message - Text Message.
      callback - Write callback.
      Returns:
      This websocket.
    • sendBinary

      @NonNull default WebSocket sendBinary(@NonNull String message)
      Send a binary message to client.
      Parameters:
      message - Binary Message.
      Returns:
      This websocket.
    • sendBinary

      @NonNull WebSocket sendBinary(@NonNull String message, @NonNull WebSocket.WriteCallback callback)
      Send a binary message to client.
      Parameters:
      message - Binary Message.
      callback - Write callback.
      Returns:
      This websocket.
    • sendBinary

      @NonNull default WebSocket sendBinary(@NonNull byte[] message)
      Send a binary message to client.
      Parameters:
      message - Binary Message.
      Returns:
      This websocket.
    • sendBinary

      @NonNull WebSocket sendBinary(@NonNull byte[] message, @NonNull WebSocket.WriteCallback callback)
      Send a binary message to client.
      Parameters:
      message - Binary Message.
      callback - Write callback.
      Returns:
      This websocket.
    • render

      @NonNull default WebSocket render(@NonNull Object value)
      Encode a value and send a text message to client.
      Parameters:
      value - Value to send.
      Returns:
      This websocket.
    • render

      @NonNull WebSocket render(@NonNull Object value, @NonNull WebSocket.WriteCallback callback)
      Encode a value and send a text message to client.
      Parameters:
      value - Value to send.
      callback - Write callback.
      Returns:
      This websocket.
    • renderBinary

      @NonNull default WebSocket renderBinary(@NonNull Object value)
      Encode a value and send a binary message to client.
      Parameters:
      value - Value to send.
      Returns:
      This websocket.
    • renderBinary

      @NonNull WebSocket renderBinary(@NonNull Object value, @NonNull WebSocket.WriteCallback callback)
      Encode a value and send a binary message to client.
      Parameters:
      value - Value to send.
      callback - Write callback.
      Returns:
      This websocket.
    • close

      @NonNull default WebSocket close()
      Close the web socket and send a WebSocketCloseStatus.NORMAL code to client.

      This method fires a WebSocket.OnClose.onClose(WebSocket, WebSocketCloseStatus) callback.

      Returns:
      This websocket.
    • close

      @NonNull WebSocket close(@NonNull WebSocketCloseStatus closeStatus)
      Close the web socket and send a close status code to client.

      This method fires a WebSocket.OnClose.onClose(WebSocket, WebSocketCloseStatus) callback.

      Parameters:
      closeStatus - Close status.
      Returns:
      This websocket.