Module org.htmlunit

Class WebSocket

java.lang.Object
org.htmlunit.corejs.javascript.SlotMapOwner<org.htmlunit.corejs.javascript.Scriptable>
org.htmlunit.corejs.javascript.ScriptableObject
All Implemented Interfaces:
Serializable, AutoCloseable, Cloneable, org.htmlunit.corejs.javascript.ConstProperties<org.htmlunit.corejs.javascript.Scriptable>, org.htmlunit.corejs.javascript.debug.DebuggableObject, org.htmlunit.corejs.javascript.PropHolder<org.htmlunit.corejs.javascript.Scriptable>, org.htmlunit.corejs.javascript.Scriptable, org.htmlunit.corejs.javascript.SymbolScriptable

public class WebSocket extends EventTarget implements AutoCloseable
JavaScript host object for WebSocket.

Ready state handling: readyState_ is read and written by the JS thread (close(), send(), getReadyState()), by the threads of the websocket adapter and by queued tasks. Every change goes through one of the three methods switchToOpen(), switchToClosing() and switchToClosed(). Each is a single atomic operation and tells the caller which state was left, so only the caller that really performed a transition fires the matching event. That gives at most one open event and exactly one close event whatever the interleaving.


   CONNECTING      -> OPEN      switchToOpen()      handshake finished
   OPEN            -> CLOSING   switchToClosing()   close() called
   CONNECTING      -> ABORTED   switchToClosing()   close() called, reported as CLOSING
   any but CLOSED  -> CLOSED    switchToClosed()    connection ended, see connectionEnded()
 

The state never moves backwards and CLOSED is final.

Author:
Ahmed Ashour, Ronald Brill, Madis Pärn
See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from class org.htmlunit.corejs.javascript.ScriptableObject

    org.htmlunit.corejs.javascript.ScriptableObject.DescriptorInfo, org.htmlunit.corejs.javascript.ScriptableObject.KeyComparator, org.htmlunit.corejs.javascript.ScriptableObject.LambdaGetterFunction, org.htmlunit.corejs.javascript.ScriptableObject.LambdaSetterFunction
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The connection has been closed or could not be opened.
    static final int
    The connection is going through the closing handshake.
    static final int
    The connection has not yet been established.
    static final int
    The WebSocket connection is established and communication is possible.

    Fields inherited from class org.htmlunit.corejs.javascript.ScriptableObject

    CONST, DONTENUM, EMPTY, LEGACY_CONST, PERMANENT, READONLY, STRICTLY_READONLY, UNINITIALIZED_CONST

    Fields inherited from class org.htmlunit.corejs.javascript.SlotMapOwner

    isSealed

    Fields inherited from interface org.htmlunit.corejs.javascript.Scriptable

    NOT_FOUND
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    void
    close(Object code, Object reason)
    Closes the WebSocket connection or connection attempt, if any.
    Returns the binary data type used when receiving binary messages.
    long
    Returns the number of bytes of data that have been queued but not yet transmitted.
    Returns the extensions negotiated with the server, or an empty string if no extensions are in use.
    org.htmlunit.corejs.javascript.Function
    Returns the event handler that fires on close.
    org.htmlunit.corejs.javascript.Function
    Returns the event handler that fires on error.
    org.htmlunit.corejs.javascript.Function
    Returns the event handler that fires on message.
    org.htmlunit.corejs.javascript.Function
    Returns the event handler that fires on open.
    Returns the sub-protocol in use, or an empty string if no sub-protocol was selected.
    int
    Returns the current state of the connection.
    Returns the URL of the WebSocket connection.
    static org.htmlunit.corejs.javascript.Scriptable
    jsConstructor(org.htmlunit.corejs.javascript.Context cx, org.htmlunit.corejs.javascript.VarScope scope, Object[] args, org.htmlunit.corejs.javascript.Function ctorObj, boolean inNewExpr)
    Creates an instance of this object.
    void
    send(Object content)
    Transmits data to the server over the WebSocket connection.
    void
    Sets the binary data type used when receiving binary messages.
    void
    setOnclose(org.htmlunit.corejs.javascript.Function closeHandler)
    Sets the event handler that fires on close.
    void
    setOnerror(org.htmlunit.corejs.javascript.Function errorHandler)
    Sets the event handler that fires on error.
    void
    setOnmessage(org.htmlunit.corejs.javascript.Function messageHandler)
    Sets the event handler that fires on message.
    void
    setOnopen(org.htmlunit.corejs.javascript.Function openHandler)
    Sets the event handler that fires on open.

    Methods inherited from class org.htmlunit.corejs.javascript.ScriptableObject

    applyDescriptorToAttributeBitset, avoidObjectDetection, buildDataDescriptor, callMethod, callMethod, checkNotSealed, checkPropertyChangeForSlot, checkPropertyChangeForSlot, checkPropertyDefinition, checkPropertyDefinition, checkSlotRemoval, defineBuiltinProperty, defineBuiltinProperty, defineBuiltInProperty, defineClass, defineClass, defineClass, defineConst, defineConstProperty, defineFunctionProperties, defineOwnProperties, defineOwnProperty, defineOwnProperty, defineOwnProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, defineProperty, delete, delete, delete, deleteProperty, deleteProperty, deleteProperty, ensureScriptable, ensureScriptableObject, ensureScriptableObjectButNotSymbol, ensureSymbolScriptable, ensureType, get, get, getAllIds, getAncestor, getArrayPrototype, getClassPrototype, getClassPrototype, getDefaultValue, getExternalArrayData, getExternalArrayLength, getFunctionPrototype, getGeneratorFunctionPrototype, getGetterOrSetter, getGetterOrSetter, getObjectPrototype, getOwnPropertyDescriptor, getParentScope, getProperty, getProperty, getProperty, getProperty, getProperty, getPropertyIds, getPrototype, getSuperProperty, getSuperProperty, getSuperProperty, getThis, getTopLevelScope, getTopScopeValue, getTypedProperty, getTypedProperty, getTypeOf, hasProperty, hasProperty, hasProperty, hasProperty, isAccessorDescriptor, isAccessorDescriptor, isConst, isDataDescriptor, isEmpty, isExtensible, isFalse, isGenericDescriptor, isGenericDescriptor, isGetterOrSetter, isGetterOrSetter, isTrue, preventExtensions, put, put, putConst, putConstProperty, putConstProperty, putOwnProperty, putOwnProperty, putOwnProperty, putProperty, putProperty, putProperty, putProperty, putSuperProperty, putSuperProperty, putSuperProperty, querySlot, redefineProperty, redefineProperty, sameValue, sealObject, setCommonDescriptorProperties, setExternalArrayData, setGetterOrSetter, setParentScope, setPrototype, size

    Methods inherited from class org.htmlunit.corejs.javascript.SlotMapOwner

    associateValue, checkNotSealed, copyAssociatedValue, createSlotMap, createSlotMap, defineProperty, defineProperty, get, getAssociatedValue, getAttributes, getAttributes, getAttributes, getAttributes, getAttributes, getIds, has, has, isSealed, readMaps, setAttributes, setAttributes, setAttributes, setAttributes, setAttributes, writeMaps

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.htmlunit.corejs.javascript.Scriptable

    get, getIds, has, has

    Methods inherited from interface org.htmlunit.corejs.javascript.SymbolScriptable

    get, has
  • Field Details

    • CONNECTING

      public static final int CONNECTING
      The connection has not yet been established.
      See Also:
    • OPEN

      public static final int OPEN
      The WebSocket connection is established and communication is possible.
      See Also:
    • CLOSING

      public static final int CLOSING
      The connection is going through the closing handshake.
      See Also:
    • CLOSED

      public static final int CLOSED
      The connection has been closed or could not be opened.
      See Also:
  • Constructor Details

    • WebSocket

      public WebSocket()
      Creates a new instance.
  • Method Details

    • jsConstructor

      public static org.htmlunit.corejs.javascript.Scriptable jsConstructor(org.htmlunit.corejs.javascript.Context cx, org.htmlunit.corejs.javascript.VarScope scope, Object[] args, org.htmlunit.corejs.javascript.Function ctorObj, boolean inNewExpr)
      Creates an instance of this object.
      Parameters:
      cx - the current context
      scope - the scope
      args - the constructor arguments
      ctorObj - the function object
      inNewExpr - whether invoked via new
      Returns:
      the new WebSocket instance
    • getOnclose

      public org.htmlunit.corejs.javascript.Function getOnclose()
      Returns the event handler that fires on close.
      Returns:
      the event handler that fires on close
    • setOnclose

      public void setOnclose(org.htmlunit.corejs.javascript.Function closeHandler)
      Sets the event handler that fires on close.
      Parameters:
      closeHandler - the event handler that fires on close
    • getOnerror

      public org.htmlunit.corejs.javascript.Function getOnerror()
      Returns the event handler that fires on error.
      Returns:
      the event handler that fires on error
    • setOnerror

      public void setOnerror(org.htmlunit.corejs.javascript.Function errorHandler)
      Sets the event handler that fires on error.
      Parameters:
      errorHandler - the event handler that fires on error
    • getOnmessage

      public org.htmlunit.corejs.javascript.Function getOnmessage()
      Returns the event handler that fires on message.
      Returns:
      the event handler that fires on message
    • setOnmessage

      public void setOnmessage(org.htmlunit.corejs.javascript.Function messageHandler)
      Sets the event handler that fires on message.
      Parameters:
      messageHandler - the event handler that fires on message
    • getOnopen

      public org.htmlunit.corejs.javascript.Function getOnopen()
      Returns the event handler that fires on open.
      Returns:
      the event handler that fires on open
    • setOnopen

      public void setOnopen(org.htmlunit.corejs.javascript.Function openHandler)
      Sets the event handler that fires on open.
      Parameters:
      openHandler - the event handler that fires on open
    • getReadyState

      public int getReadyState()
      Returns the current state of the connection. Possible values are CONNECTING, OPEN, CLOSING, or CLOSED.
      Returns:
      the current ready state
    • getUrl

      public String getUrl()
      Returns the URL of the WebSocket connection.
      Returns:
      the URL string
    • getProtocol

      public String getProtocol()
      Returns the sub-protocol in use, or an empty string if no sub-protocol was selected.
      Returns:
      the sub-protocol
    • getExtensions

      public String getExtensions()
      Returns the extensions negotiated with the server, or an empty string if no extensions are in use. The adapter does not report the negotiated extensions, so this is always an empty string.
      Returns:
      the extensions in use
    • getBufferedAmount

      public long getBufferedAmount()
      Returns the number of bytes of data that have been queued but not yet transmitted. Once the connection is closing or closed this value only increases: every send() adds the size of its data, which is discarded. Data sent while the connection is open is handed over to the adapter at once and is not counted, because the adapter does not tell when the data has left.
      Returns:
      the buffered amount in bytes
    • getBinaryType

      public String getBinaryType()
      Returns the binary data type used when receiving binary messages.
      Returns:
      the binary type ("blob" or "arraybuffer")
    • setBinaryType

      public void setBinaryType(String type)
      Sets the binary data type used when receiving binary messages.
      Parameters:
      type - the new binary type; must be "blob" or "arraybuffer"
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • close

      public void close(Object code, Object reason)
      Closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSING or CLOSED, this method does nothing. The arguments are validated first, whatever the state is: a code that is neither 1000 nor in the range 3000 to 4999 results in an InvalidAccessError, a reason longer than 123 bytes (UTF-8) in a SyntaxError.
      Parameters:
      code - a numeric value indicating the status code explaining why the connection is being closed
      reason - a human-readable string explaining why the connection is closing
    • send

      public void send(Object content)
      Transmits data to the server over the WebSocket connection. Throws an InvalidStateError while CONNECTING; the data is discarded when CLOSING or CLOSED. Anything that is not binary is converted to a string and sent as text message. Binary data (an ArrayBuffer, a typed array or DataView, which contribute only the part they view, or a Blob) is sent as binary message; it is copied before this method returns, so later changes of the buffer do not change what is sent.
      Parameters:
      content - the data to send