Class MessageHandler

java.lang.Object
org.eclipse.jetty.websocket.core.internal.MessageHandler
All Implemented Interfaces:
FrameHandler, IncomingFrames

public class MessageHandler extends Object implements FrameHandler

A utility implementation of FrameHandler that aggregates TEXT frames into a String message before calling onText(String, Callback).

  • Constructor Details

    • MessageHandler

      public MessageHandler()
  • Method Details

    • from

      public static MessageHandler from(Consumer<String> onText, Consumer<ByteBuffer> onBinary)
    • getCoreSession

      public CoreSession getCoreSession()
    • onOpen

      public void onOpen(CoreSession coreSession, Callback callback)
      Description copied from interface: FrameHandler

      Invoked when the WebSocket connection is opened.

      It is allowed to send WebSocket frames via OutgoingFrames.sendFrame(Frame, Callback, boolean).

      WebSocket frames cannot be received until a call to CoreSession.demand() is made.

      If the callback argument is failed, the implementation sends a CLOSE frame with CloseStatus.SERVER_ERROR, and the connection will be closed.

      Specified by:
      onOpen in interface FrameHandler
      Parameters:
      coreSession - the session associated with this connection.
      callback - the callback to indicate success or failure of the processing of this event.
    • onFrame

      public void onFrame(Frame frame, Callback callback)
      Description copied from interface: FrameHandler

      Invoked when a WebSocket frame is received.

      This method will never be called concurrently for the same session; will be called sequentially to satisfy the outstanding demand signaled by calls to CoreSession.demand().

      Both control and data frames are passed to this method.

      CLOSE frames may be responded from this method, but if they are not responded, then the implementation will respond when the callback is completed.

      The callback argument must be completed to indicate that the buffers associated with the frame can be recycled.

      Additional WebSocket frames (of any type, including CLOSE frames) cannot be received until a call to CoreSession.demand() is made.

      Specified by:
      onFrame in interface FrameHandler
      Specified by:
      onFrame in interface IncomingFrames
      Parameters:
      frame - the WebSocket frame.
      callback - the callback to indicate success or failure of the processing of this event.
    • onError

      public void onError(Throwable cause, Callback callback)
      Description copied from interface: FrameHandler

      Invoked when an error has occurred or has been detected.

      A call to this method will be followed by a call to FrameHandler.onClosed(CloseStatus, Callback) with the close status derived from the error.

      This method will not be called more than once, FrameHandler.onClosed(CloseStatus, Callback) will be called on the callback completion.

      Specified by:
      onError in interface FrameHandler
      Parameters:
      cause - the error cause
      callback - the callback to indicate success or failure of the processing of this event.
    • onClosed

      public void onClosed(CloseStatus closeStatus, Callback callback)
      Description copied from interface: FrameHandler

      Invoked when a WebSocket close event happened.

      The WebSocket connection is closed, no reading or writing is possible anymore.

      Implementations of this method may cleanup resources that have been allocated.

      This method will not be called more than once.

      Specified by:
      onClosed in interface FrameHandler
      Parameters:
      closeStatus - the close status received from the remote peer, or generated locally in the case of abnormal closures.
      callback - the callback to indicate success or failure of the processing of this event.
    • onTextFrame

      protected void onTextFrame(Frame frame, Callback callback)
    • onBinaryFrame

      protected void onBinaryFrame(Frame frame, Callback callback)
    • onContinuationFrame

      protected void onContinuationFrame(Frame frame, Callback callback)
    • onPingFrame

      protected void onPingFrame(Frame frame, Callback callback)
    • onPongFrame

      protected void onPongFrame(Frame frame, Callback callback)
    • onCloseFrame

      protected void onCloseFrame(Frame frame, Callback callback)
    • onText

      protected void onText(String message, Callback callback)
      Method called when a complete text message is received.
      Parameters:
      message - the received text payload
      callback - The callback to signal completion of handling.
    • onBinary

      protected void onBinary(ByteBuffer message, Callback callback)
      Method called when a complete binary message is received.
      Parameters:
      message - The binary payload
      callback - The callback to signal completion of handling.
    • sendText

      public void sendText(String message, Callback callback, boolean batch)
      Send a String as a single text frame.
      Parameters:
      message - The message to send
      callback - The callback to call when the send is complete
      batch - The batch mode to send the frames in.
    • sendText

      public void sendText(Callback callback, boolean batch, String... parts)
      Send a sequence of Strings as a sequences for fragmented text frame. Sending a large message in fragments can reduce memory overheads as only a single fragment need be converted to bytes
      Parameters:
      callback - The callback to call when the send is complete
      batch - The batch mode to send the frames in.
      parts - The parts of the message.
    • sendBinary

      public void sendBinary(ByteBuffer message, Callback callback, boolean batch)
      Send a ByteBuffer as a single binary frame.
      Parameters:
      message - The message to send
      callback - The callback to call when the send is complete
      batch - The batch mode to send the frames in.
    • sendBinary

      public void sendBinary(Callback callback, boolean batch, ByteBuffer... parts)
      Send a sequence of ByteBuffers as a sequences for fragmented text frame.
      Parameters:
      callback - The callback to call when the send is complete
      batch - The batch mode to send the frames in.
      parts - The parts of the message.