# 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 Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `WebSocketImpl(String url)`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `abstract void`
  `close()`
  Close the connection.
  `abstract void`
  `connect(int connectTimeoutMs)`
  Initiate the connection.
  `abstract WebSocketState`
  `getReadyState()`
  `String`
  `getSelectedSubprotocol()`
  The subprotocol the server selected, or null when none was negotiated (or the connection has not completed yet).
  `final String`
  `getUrl()`
  `protected final String[]`
  `requestedSubprotocols()`
  The subprotocols the client offered, or null when none were set.
  `abstract void`
  `sendBinary(byte[] message)`
  `abstract void`
  `sendText(String message)`
  `final void`
  `setEventSink(WebSocketEventSink sink)`
  `final void`
  `setRequestedSubprotocols(String[] protocols)`
  Sets the subprotocols (RFC 6455 `Sec-WebSocket-Protocol`) the client offers, in preference order.
  `protected final void`
  `setSelectedSubprotocol(String protocol)`
  Records the subprotocol the server selected.
  `protected final WebSocketEventSink`
  `sink()`

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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()