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 defragments text frames into a String message before calling onText(String, Callback). Flow control is by default automatic, but an implementation may extend FrameHandler.isDemanding() to return true and then explicityly control demand with calls to CoreSession.demand(long)
  • 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
      Async notification that Connection is being opened.

      FrameHandler can write during this call, but can not receive frames until the callback is succeeded.

      If the FrameHandler succeeds the callback we transition to OPEN state and can now receive frames if not demanding, or can now call CoreSession.demand(long) to receive frames if demanding. If the FrameHandler fails the callback a close frame will be sent 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 in processing (or failure)
    • onFrame

      public void onFrame(Frame frame, Callback callback)
      Description copied from interface: FrameHandler
      Receiver of all Frames. This method will never be called in parallel for the same session and will be called sequentially to satisfy all outstanding demand signaled by calls to CoreSession.demand(long). Control and Data frames are passed to this method. Close frames may be responded to by the handler, but if an appropriate close response is not sent once the callback is succeeded, then a response close will be generated and sent.
      Specified by:
      onFrame in interface FrameHandler
      Specified by:
      onFrame in interface IncomingFrames
      Parameters:
      frame - the raw frame
      callback - the callback to indicate success in processing frame (or failure)
    • onError

      public void onError(Throwable cause, Callback callback)
      Description copied from interface: FrameHandler
      An error has occurred or been detected in websocket-core and being reported to FrameHandler. A call to onError will be followed by a call to FrameHandler.onClosed(CloseStatus, Callback) giving the close status derived from the error. This 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 reason for the error
      callback - the callback to indicate success in processing (or failure)
    • onClosed

      public void onClosed(CloseStatus closeStatus, Callback callback)
      Description copied from interface: FrameHandler
      This is the Close Handshake Complete event.

      The connection is now closed, no reading or writing is possible anymore. Implementations of FrameHandler can cleanup their resources for this connection now. This method will be called only once.

      Specified by:
      onClosed in interface FrameHandler
      Parameters:
      closeStatus - the close status received from remote, or in the case of abnormal closure from local.
      callback - the callback to indicate success in processing (or failure)
    • 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.