Package 

Interface WebSocket


  • 
    public interface WebSocket
    
                        

    A non-blocking interface to a web socket. Use the factory to create instances; usually this is MobonOkHttpClient.

    Web Socket LifecycleUpon normal operation each web socket progresses through a sequence of states:
    • Connecting: the initial state of each web socket. Messages may be enqueued but they won't be transmitted until the web socket is open.
    • Open: the web socket has been accepted by the remote peer and is fully operational. Messages in either direction are enqueued for immediate transmission.
    • Closing: one of the peers on the web socket has initiated a graceful shutdown. The web socket will continue to transmit already-enqueued messages but will refuse to enqueue new ones.
    • Closed: the web socket has transmitted all of its messages and has received all messages from the peer.
    Web sockets may fail due to HTTP upgrade problems, connectivity problems, or if either peer chooses to short-circuit the graceful shutdown process:
    • Canceled: the web socket connection failed. Messages that were successfully enqueued by either peer may not have been transmitted to the other.
    Note that the state progression is independent for each peer. Arriving at a gracefully-closed state indicates that a peer has sent all of its outgoing messages and received all of its incoming messages. But it does not guarantee that the other peer will successfully receive all of its incoming messages.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public interface WebSocket.Factory
    • Method Summary

      Modifier and Type Method Description
      abstract MobonRequest request() Returns the original mobonRequest that initiated this web socket.
      abstract long queueSize() Returns the size in bytes of all messages enqueued to be transmitted to the server.
      abstract boolean send(String text) Attempts to enqueue {@code text} to be UTF-8 encoded and sent as a the data of a text (type {@code 0x1}) message.
      abstract boolean send(ByteString bytes) Attempts to enqueue {@code bytes} to be sent as a the data of a binary (type {@code 0x2})message.
      abstract boolean close(int code, @Nullable() String reason) Attempts to initiate a graceful shutdown of this web socket.
      abstract void cancel() Immediately and violently release resources held by this web socket, discarding any enqueuedmessages.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • request

         abstract MobonRequest request()

        Returns the original mobonRequest that initiated this web socket.

      • queueSize

         abstract long queueSize()

        Returns the size in bytes of all messages enqueued to be transmitted to the server. Thisdoesn't include framing overhead. It also doesn't include any bytes buffered by the operatingsystem or network intermediaries. This method returns 0 if no messages are waitingin the queue. If may return a nonzero value after the web socket has been canceled; thisindicates that enqueued messages were not transmitted.

      • send

         abstract boolean send(String text)

        Attempts to enqueue {@code text} to be UTF-8 encoded and sent as a the data of a text (type {@code 0x1}) message.

        This method returns true if the message was enqueued. Messages that would overflow theoutgoing message buffer will be rejected and trigger a graceful shutdown ofthis web socket. This method returns false in that case, and in any other case where thisweb socket is closing, closed, or canceled.

        This method returns immediately.

      • send

         abstract boolean send(ByteString bytes)

        Attempts to enqueue {@code bytes} to be sent as a the data of a binary (type {@code 0x2})message.

        This method returns true if the message was enqueued. Messages that would overflow theoutgoing message buffer (16 MiB) will be rejected and trigger a graceful shutdown of this web socket. This method returns false in thatcase, and in any other case where this web socket is closing, closed, or canceled.

        This method returns immediately.

      • close

         abstract boolean close(int code, @Nullable() String reason)

        Attempts to initiate a graceful shutdown of this web socket. Any already-enqueued messages willbe transmitted before the close message is sent but subsequent calls to send willreturn false and their messages will not be enqueued.

        This returns true if a graceful shutdown was initiated by this call. It returns false and ifa graceful shutdown was already underway or if the web socket is already closed or canceled.

        Parameters:
        code - Status code as defined by Section 7.4 of RFC 6455.
        reason - Reason for shutting down or {@code null}.
      • cancel

         abstract void cancel()

        Immediately and violently release resources held by this web socket, discarding any enqueuedmessages. This does nothing if the web socket has already been closed or canceled.