Skip to main content

Base adapter for devices that can be controlled via a TCP socket

Works with
This package works with Deno
JSR Score
100%
Published
4 weeks ago (1.0.2)

@Deno-PLC / Adapter-TCP

Base adapter for devices that can be controlled via a TCP socket#

Installation

$ deno add @deno-plc/adapter-tcp

Usage

import {
    TCPAdapter,
    TCPAdapterCallback,
    TCPAdapterSession,
} from "@deno-plc/adapter-tcp";

interface MyProtocolAdapterOptions {
    host: string;
    port?: number;
    verbose?: boolean;
}

class MyProtocolAdapter extends TCPAdapter {
    constructor(options: MyProtocolAdapterOptions) {
        super({
            sessionFactory: (cb) => new MyProtocolAdapterSession(this, cb),
            port: 1234,
            ...options,
        });
    }
}

class MyProtocolAdapterSession implements TCPAdapterSession {
    constructor(readonly adapter: MyProtocolAdapter, send: TCPAdapterCallback) {
        this.#send = send;
        setTimeout(() => {
            // TX
            this.#send(new TextEncoder().encode("Hello"));
        });
    }
    readonly #send: TCPAdapterCallback;
    recv(data: Uint8Array): void {
        // RX
    }
    destroy(): void {
        // cleanup
    }
}

For more see examples/

License

Copyright (C) 2024 Hans Schallmoser

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Built and signed on
GitHub Actions
View transparency log

Add Package

deno add @deno-plc/adapter-tcp

Import symbol

import * as mod from "@deno-plc/adapter-tcp";