Skip to main content
Home

Built and signed on GitHub Actions

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

This package works with Deno
This package works with Deno
JSR Score
100%
Published
4 months ago (1.1.0)

Deno-PLC / Adapter-TCP

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

Installation

Use JSR: JSR

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,
            label: `MyProtocol ${options.host}`,
            ...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/

Logging

All events (connect/disconnect/errors) can be logged with logtape. You can disable it by setting verbose = false, simply not configuring logtape or setting a log level > info for app>deno-plc>tcp>[options.label]

License

Copyright (C) 2024 - 2025 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

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@deno-plc/adapter-tcp

Import symbol

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

Import directly with a jsr specifier

import * as adapter_tcp from "jsr:@deno-plc/adapter-tcp";