Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Channel<ChannelType>

Wraps a channel on a client bus.

example
import { Client } from '@cast-web/protocol';
import {
  ConnectionChannel,
  HeartbeatChannel,
  ReceiverChannel,
  Namespaces,
} from '@cast-web/types';

const client = new Client();
// wait for the client to connect
await client.connect({ host: '192.168.1.101' });

// create channels
const connection = client.createChannel<ConnectionChannel>('sender-0', 'receiver-0', Namespaces.Connection, 'JSON');
const heartbeat = client.createChannel<HeartbeatChannel>('sender-0', 'receiver-0', Namespaces.Heartbeat, 'JSON');
const receiver = client.createChannel<ReceiverChannel>('sender-0', 'receiver-0', Namespaces.Receiver, 'JSON');

// listen to channel events (receiver as an example)
receiver.on('connect', () => console.log('receiver connect'));
receiver.on('close', () => console.log('receiver close'));
receiver.on('error', error => console.log('receiver error:', error));
receiver.on('message', message => console.log('receiver message:', message));

// connect to the receiver
connection.send({ type: 'CONNECT' });

// start the heartbeat
setInterval(() => heartbeat.send({ type: 'PING' }), 5000);

Type parameters

  • ChannelType: ConnectionChannel | HeartbeatChannel | MediaChannel | ReceiverChannel

Hierarchy

Index

Constructors

Methods

Constructors

constructor

  • new Channel<ChannelType>(bus: Client, sourceId: string, destinationId: string, namespace: Namespaces, encoding: "JSON"): Channel<ChannelType>
  • Type parameters

    • ChannelType: ConnectionChannel | HeartbeatChannel | MediaChannel | ReceiverChannel

    Parameters

    • bus: Client
    • sourceId: string
    • destinationId: string
    • namespace: Namespaces
    • encoding: "JSON"

    Returns Channel<ChannelType>

Methods

close

  • close(): void
  • Closes the channel

    remarks

    This is important to prevent EventEmitter leaks.

    Returns void

send

  • send(data: ChannelType["data"]): void
  • Sends data on the channel.

    example
    // ConnectionChannel
    connection.send({ type: 'CONNECT' });
    

    Parameters

    • data: ChannelType["data"]

      depend on channel type

    Returns void

    void

Static decode

  • decode(data: any, encoding: "JSON"): any
  • Decodes data received on a channel

    Parameters

    • data: any
    • encoding: "JSON"

    Returns any

    JS expresion of the data.

Static encode

  • encode(data: any, encoding: "JSON"): any
  • Encodes data to be sent on a channel.

    Parameters

    • data: any
    • encoding: "JSON"

    Returns any

    Encoded data.

Generated using TypeDoc