Skip to content

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

License

Notifications You must be signed in to change notification settings

deno-plc/adapter-tcp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published