Class WebSocketImpl

java.lang.Object
com.codename1.impl.WebSocketImpl

public abstract class WebSocketImpl extends Object

Platform contract behind the public com.codename1.io.WebSocket facade. Each port that supports WebSocket subclasses this and is returned from CodenameOneImplementation.createWebSocketImpl(String).

Subclasses call sink().onConnect(), sink().onTextMessage(...), etc. from whichever thread the native layer fires the event on. The sink is set exactly once, by the facade, before connect(int) is called.

  • Constructor Details

    • WebSocketImpl

      protected WebSocketImpl(String url)
  • Method Details

    • getUrl

      public final String getUrl()
    • setEventSink

      public final void setEventSink(WebSocketEventSink sink)
    • sink

      protected final WebSocketEventSink sink()
    • setRequestedSubprotocols

      public final void setRequestedSubprotocols(String[] protocols)
      Sets the subprotocols (RFC 6455 Sec-WebSocket-Protocol) the client offers, in preference order. Called by the public facade before connect(int); ports include them in the handshake. Null or empty means "no subprotocol negotiation".
    • requestedSubprotocols

      protected final String[] requestedSubprotocols()
      The subprotocols the client offered, or null when none were set. Ports read this while building the handshake.
    • setSelectedSubprotocol

      protected final void setSelectedSubprotocol(String protocol)
      Records the subprotocol the server selected. Ports call this once the handshake completes, before firing sink().onConnect(), so the value is visible to the user's connect handler.
    • getSelectedSubprotocol

      public String getSelectedSubprotocol()
      The subprotocol the server selected, or null when none was negotiated (or the connection has not completed yet).
    • connect

      public abstract void connect(int connectTimeoutMs)
      Initiate the connection. May return immediately and complete asynchronously; success is signalled via sink().onConnect().
      Parameters:
      connectTimeoutMs - handshake timeout in milliseconds, or 0 for the platform default.
    • close

      public abstract void close()
      Close the connection. Idempotent -- calling on an already-closed connection is a no-op.
    • sendText

      public abstract void sendText(String message)
    • sendBinary

      public abstract void sendBinary(byte[] message)
    • getReadyState

      public abstract WebSocketState getReadyState()