Options
All
  • Public
  • Public/Protected
  • All
Menu

Class XHRTransport

The implementation of the Transport interface for XHR polling

Hierarchy

Implements

Index

Constructors

constructor

  • Construct a new XHRTransport instance

    Parameters

    • opts: OptionsImpl

      the connection options

    • xhr: {}

      the transport constructor function

    Returns XHRTransport

Properties

Private URI

URI: string

The connection URI

Private aborted

aborted: boolean

A flag indicating whether sending of poll requests has been aborted

Private Readonly emitter

emitter: Emitter

The emitter used for emitting events

Private isSending

isSending: boolean

A flag indicating that the transport is currently sending requests

Private opts

The connection options

Private pingSequence

pingSequence: number

A sequence number for ping requests

Private pollRequest

pollRequest: any

The last poll request

Private pollSequence

pollSequence: number

A sequence number for polling requests

Private protocolVersion

protocolVersion: number | null

The protocol version

Private queue

queue: Queue<Uint8Array>

The message queue

Private token

token: SessionToken | null

The session token that was received from the server at connection

Private Readonly xhr

xhr: {}

The transport constructor function

Type declaration

Methods

Private canRetry

  • canRetry(status: number, attempt: number, maxAttempts: number): boolean
  • Whether a failed request should be sent again.

    A status of 0 is the browser reporting a transport-level failure - a request that was aborted, blocked or never completed - rather than a response the server sent. Any other status is an answer from the server and repeating the request will not change it.

    Parameters

    • status: number

      the status of the failed request

    • attempt: number

      the attempt number that just failed, counting from 1

    • maxAttempts: number

      the number of attempts this kind of request gets

    Returns boolean

close

  • close(): this
  • Aborting any opening poll request.

    Returns this

    this instance

connect

  • Establish a polling connection with the server.

    Parameters

    Returns void

createRequest

  • createRequest(headers: {}, uri: string): any
  • This is the generic method to send data to the server

    Parameters

    • headers: {}

      the headers to send to the server

      • [header: string]: any
    • uri: string

      the URI to connect to

    Returns any

dispatch

  • dispatch(message: Uint8Array): boolean | undefined
  • Queue message to be sent to the message message-queue.

    Delegate the actual request sending to #flush().

    Parameters

    • message: Uint8Array

      to be queued

    Returns boolean | undefined

error

  • Close the stream. This will emit a 'close' event to any assigned listeners. No further events will be emitted.

    Parameters

    Returns Stream

flush

  • flush(): void
  • Flush all pending messages from the message-queue and send upstream.

    Returns void

off

  • Remove a listener from a specified event.

    Example:

    // Bind a single listener to the 'foo' event and then deregister it
    var listener = function() {};
    stream.on('foo', listener);
    stream.off('foo', listener);
    

    Example:

    // Bind a listener to the 'foo' event and deregister all listeners
    var listener = function() {};
    stream.on('foo', listener);
    stream.off('foo');
    

    Parameters

    • events: string | CallbackMap
    • Optional listener: StreamCallback

      the listener to remove. All listeners for the event are removed if this is not specified. This argument is ignored if the first argument is a CallbackMap.

    Returns Stream

    this stream.

on

  • Register listeners against events.

    A single listener may be bound to an event by passing the event name and listener function.

    Multiple listeners may be bound by passing in a CallbackMap, mapping event names to listener functions.

    Example:

    // Bind a single listener to the 'foo' event
    stream.on('foo', function(arg1, arg2) {
        console.log("Called for 'foo' event", arg1, arg2);
    });
    

    Example:

    // Bind multiple listeners
    stream.on({
        foo : function() { ... },
        bar : function() { ... },
        baz : function() { ... }
    });
    

    It is possible to bind multiple listeners to the same event. In this case, the listeners will be called in the order they were bound.

    Parameters

    • events: string | CallbackMap

      the event name or CallbackMap mapping event names to listeners

    • Optional listener: StreamCallback

      the listener to bind to the event, if passed as string. This argument is ignored if the first argument is a CallbackMap.

    Returns Stream

    this stream.

poll

  • poll(): void
  • Send a poll request to the server.

    Returns void

Private reportUnexpectedClose

  • reportUnexpectedClose(kind: string, status: number, attempt: number, maxAttempts: number): void
  • Report a request failure that closes the session.

    The status and the attempt it failed on are the only evidence of why the transport gave up rather than retried, and neither survives anywhere else. An aborted request is an expected close, so it is not reported.

    Parameters

    • kind: string

      'Poll' or 'Send', naming the request that failed

    • status: number

      the status of the failed request

    • attempt: number

      the attempt number that just failed, counting from 1

    • maxAttempts: number

      the number of attempts that kind of request gets

    Returns void

Private scheduleRetry

  • scheduleRetry(attempt: number, send: () => void): void
  • Schedule the next attempt at a failed request.

    Parameters

    • attempt: number

      the attempt number that just failed, counting from 1

    • send: () => void

      sends the next attempt

        • (): void
        • Returns void

    Returns void

Private sendMessages

  • sendMessages(buffer: Uint8Array, sequence: number, attempt: number): void
  • Send drained messages to the server.

    A retry repeats the original sequence number. The server orders these requests and closes the channel if a gap is not filled within its message-sequence-timeout, while a request it has already handled is dropped as a duplicate - so repeating the sequence is correct whether or not the failed attempt arrived.

    Parameters

    • buffer: Uint8Array

      the drained messages

    • sequence: number

      the sequence number for this request

    • attempt: number

      the attempt number for this request, counting from 1

    Returns void

Private sendPoll

  • sendPoll(attempt: number): void
  • Send a poll request to the server.

    The server ignores a poll's sequence number, so a retry simply takes the next one.

    Parameters

    • attempt: number

      the attempt number for this poll, counting from 1

    Returns void

Copyright © 2025 DiffusionData Ltd. All Rights Reserved.