Menu

class: WebSocket

[17:14] (extern: com.aussom.aussomscript.AWebSocket) extends: object

The WebSocket class provides a full-duplex communication channel between an Aussom script and a WebSocket server. Both text and binary messages are supported. Binary data is exchanged as Aussom 'buffer' objects. ReadyState constants: 0 = CONNECTING The connection is being established. 1 = OPEN The connection is established and messages can be sent. 2 = CLOSING The connection is being closed. 3 = CLOSED The connection is closed or could not be opened. Requires a secure context (HTTPS or localhost) in most production browsers. On plain file:// URLs, WebSocket connections to wss:// endpoints still work in most browsers because the restriction applies to mixed-content requests, not WebSocket itself.

Methods

  • _init (string Url, string Protocol = null)

  • WebSocket (string Url, string Protocol = null)

    Creates a new WebSocket and begins connecting to the given URL. The connection completes asynchronously; register an "open" listener to know when the socket is ready to send messages.

    • @p Url is the WebSocket server URL, starting with ws:// or wss://.
    • @p Protocol is an optional WebSocket sub-protocol string.
    • @r this object
  • send (string Text)

    Sends a UTF-8 text message to the server. The connection must be open (getReadyState() == 1) before calling send.

    • @p Text is the string message to send.
    • @r this object
  • sendBytes (object Buf)

    Sends the contents of an Aussom buffer as a binary message to the server. The connection must be open (getReadyState() == 1) before calling sendBytes.

    • @p Buf is a buffer object containing the bytes to transmit.
    • @r this object
  • close (int Code = 1000, string Reason = null)

    Initiates a closing handshake for the connection. The "close" event fires asynchronously when the handshake completes.

    • @p Code is the WebSocket close status code. Defaults to 1000 (normal closure). Valid application codes are 1000 and 3000-4999.
    • @p Reason is a human-readable reason string (max 123 bytes UTF-8).
    • @r this object
  • getReadyState ()

    Returns the current connection state as an integer. 0 = CONNECTING, 1 = OPEN, 2 = CLOSING, 3 = CLOSED

    • @r int
  • getUrl ()

    Returns the URL this socket is connected to.

    • @r string
  • getBinaryType ()

    Returns the binary type setting for incoming binary messages. Aussom always uses "arraybuffer" internally so binary messages are delivered as buffer objects.

    • @r string
  • setBinaryType (string Type)

    Sets the binary type for incoming binary messages ("blob" or "arraybuffer"). Aussom sets this to "arraybuffer" automatically in the constructor. Changing it to "blob" will break binary message delivery.

    • @p Type is "blob" or "arraybuffer".
    • @r this object
  • addListener (string Event, callback Handler, object PassedArg = null)

    Registers a callback for a WebSocket lifecycle event. Supported events and callback signatures: "open" Fires when the connection is established and ready to send messages. Handler(eventName, null, PassedArg) "message" Fires when a message is received from the server. Handler(eventName, data, PassedArg) data is a string for text messages or a buffer for binary messages. "close" Fires when the connection is closed. Handler(eventName, info, PassedArg) info is a map with keys: code (int) -- WebSocket close status code reason (string) -- human-readable close reason wasClean (bool) -- true if the connection closed cleanly "error" Fires when a connection error occurs. Handler(eventName, null, PassedArg) Note: The buffer object delivered to "message" handlers for binary messages is reused across messages on the same listener. Copy the buffer contents before the next message arrives if you need to accumulate multiple binary messages.

    • @p Event is the event name string ("open", "message", "close", "error").
    • @p Handler is the callback function.
    • @p PassedArg is an optional value forwarded as the third callback argument.
    • @r this object
  • isSupported ()

    Returns true if the WebSocket API is available in the current browser or runtime environment.

    • @r bool