Class WsMessageContext<Context>

Type Parameters

  • Context extends Record<any, any> = {}

Hierarchy (view full)

Constructors

Properties

@: Context = ...

Context Variables that are available anywhere in the requests lifespan

Since

1.2.1

abort: AbortSignal
client: {
    internal: boolean;
    ip: IPAddress<4 | 6>;
    origin: string;
    port: number;
    proxied: boolean;
    referrer: string;
    userAgent: string;
}

Client Infos

Type declaration

  • Readonly internal: boolean

    Whether the Client IP Address is from an internal fetch

    Since

    9.3.0

  • Readonly ip: IPAddress<4 | 6>

    The IP Address that the Client is using

    When a valid Proxy Request is made (and proxy is enabled) this will be the proper IP

    Since

    3.0.0

  • Readonly origin: string

    The Origin of the Clients Request

    Since

    9.5.0

    Example

    localhost:8000
    
  • Readonly port: number

    The Port that the Client is using

    Since

    3.0.0

  • Readonly proxied: boolean

    Whether the Client IP Address is proxied

    Since

    9.3.0

  • Readonly referrer: string

    The Referrer of the Clients Request

    Since

    9.5.0

    Example

    https://localhost:8000/me
    
  • Readonly userAgent: string

    The User Agent of the Client

    Since

    3.0.0

context: RequestContext<any>

The Request Context Object used by the server

Since

9.0.0

The Global Context Object used by the server

Since

9.0.0

headers: ValueCollection<string, string, Content>

A Collection of all Headers

Example

if (ctr.headers.has('authorization')) console.log('Authorization Header is present')

console.log(ctr.headers.get('authorization')) // Will print undefined if not present
console.log(ctr.headers.get('authorization', 'hello')) // Will print 'hello' if not present

Since

2.0.0

params: BaseCollection<string, string>

A Collection of all Path Parameters

Example

console.log(ctr.params.get('server')) // Will print undefined if not present

Since

2.0.0

server: Server<{}, [], {}>

The Server Object that initialized this Request

Type declaration

    Type declaration

      Since

      9.8.0

      type: "message"

      The Type of this Websocket Event

      Since

      5.7.0

      url: default

      The Requested URL

      Since

      0.0.2

      Accessors

      • get cookies(): ValueCollection<string, string, Cookie>
      • A Collection of all Client Cookies

        Returns ValueCollection<string, string, Cookie>

        Example

        import { Cookie } from "rjweb-server"

        if (ctr.cookies.has('theme')) console.log('Theme Cookie is present')

        console.log(ctr.cookies.get('theme')) // Will print undefined if not present
        console.log(ctr.cookies.get('theme', 'light')) // Will print 'light' if not present

        ctr.cookies.set('session', new Cookie(Math.random(), {
        path: '/'
        }))

        Since

        2.0.0

      • get fragments(): BaseCollection<string, string>
      • A Collection of all URL Fragments

        Returns BaseCollection<string, string>

        Example

        if (ctr.fragments.has('user')) console.log('User Fragment is present')

        console.log(ctr.fragments.get('user')) // Will print undefined if not present
        console.log(ctr.fragments.get('user', 'default')) // Will print 'default' if not present

        Since

        7.0.0

      • get queries(): BaseCollection<string, string>
      • A Collection of all URL Queries

        Returns BaseCollection<string, string>

        Example

        if (ctr.queries.has('user')) console.log('User Query is present')

        console.log(ctr.queries.get('user')) // Will print undefined if not present
        console.log(ctr.queries.get('user', 'default')) // Will print 'default' if not present

        Since

        2.0.0

      Methods

      • Grab a Channel from either a string identifier or a Channel object

        Type Parameters

        Parameters

        Returns Channel<C>

        Example

        const channel = ctr.$channel('channel')

        await channel.send('text', 'Ok')

        // or

        const ref = new Channel<string>()
        const channel = ctr.$channel(ref)

        await channel.send('text', 'Ok')

        Since

        9.8.0

      • Clear the active Ratelimit of the Client

        This Clears the currently active Ratelimit (on this socket) of the Client, remember: you cant call this in a normal message callback if the max hits are already reached since well... they are already reached.

        Returns this

        Since

        8.6.0

      • Close the Socket and send a Code + Message to the Client (automatically Formatted)

        This will instantly close the socket connection with a status code and message of choice, after calling and successfully closing the .onClose() callback will be called to finish the task.

        Parameters

        • Optional code: number
        • Optional reason: string

        Returns this

        Example

        ctr.close(1011, 'An Error has occured')
        

        Since

        5.4.0

      • Print a Message to the Client (automatically Formatted)

        This Message will instantly sent to the client, since this is a websocket, this also means that the message cannot be overriden after this function is called.

        Parameters

        • type: "binary" | "text"
        • content: Content
        • prettify: boolean = false

        Returns Promise<WsMessageContext<Context>>

        Example

        await ctr.print({
        message: 'this is json!'
        })

        // content will be `{"message":"this is json!"}`

        /// or

        await ctr.print({
        message: 'this is json!'
        }, true)
        // content will be `{\n "message": "this is json!"\n}`

        /// or

        await ctr.print('this is text!')
        // content will be `this is text!`

        Since

        5.4.0

      • Print a channels value to the client

        This will print when the provided channel has a new value, basically subscribing to the channel.

        Parameters

        Returns this

        Example

        const channel = new Channel<string>()

        ctr.printChannel(channel)

        ref.send('text', 'Ok')

        Since

        9.0.0

      • Print a Raw Message to the Client (ArrayBuffer only)

        Same as .print() but only accepts ArrayBuffer as the content and therefore skips the parsing process. This is useful for sending data synchronously.

        Parameters

        • type: "binary" | "text"
        • content: ArrayBuffer

        Returns this

        Example

        const buffer = new ArrayBuffer(10)

        ctr.printRaw('binary', buffer)

        Since

        9.8.0

      • Print a raw channels value to the client

        This will print when the provided channel has a new value, basically subscribing to the channel. This uses strings to identify the channel instead of the channel object. (CAUTION)

        Parameters

        • channel: string

        Returns this

        Example

        ctr.printRawChannel('channel')

        await ctr.$channel('channel').print('text', 'Ok')

        Since

        9.8.0

      • Remove a channel from the client

        This will remove the subscription to the channel from the client. No more messages will be sent.

        Parameters

        Returns this

        Example

        const channel = new Channel<string>()

        ctr.printChannel(channel)

        ref.send('Ok')

        ctr.removeChannel(channel)

        ref.send('No') // will not be sent

        Since

        9.0.0

      • Remove a raw channel from the client

        This will remove the subscription to the channel from the client. No more messages will be sent. This uses strings to identify the channel instead of the channel object. (CAUTION)

        Parameters

        • channel: string

        Returns this

        Example

        ctr.printRawChannel('channel')

        await ctr.$channel('channel').print('text', 'Ok')

        ctr.removeRawChannel('channel')

        await ctr.$channel('channel').print('text', 'No') // will not be sent

        Since

        9.8.0

      • Skips counting the request to the Client IPs Rate limit (if there is one)

        When a specific IP makes sends a message to an endpoint under a ratelimit, the maxhits will be increased instantly to prevent bypassing the rate limit by spamming messages faster than the host can handle. When this function is called, the server removes the set hit again.

        Returns this

        Since

        8.6.0