npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tcp-controller

v2.0.2

Published

A simple TCP controller framework. With TCP Controller you can manage your TCP server more easily.

Downloads

18

Readme

Getting started into TCP Controller!

  1. @TcpController([address, port])
  2. @TcpMessageStream()
  3. @TcpMessage()
  4. @TcpError()
  5. @TcpListening()
  6. @TcpClientDisconnected()
  7. @TcpClientConnected()
  8. @TcpCriteria([id])
  9. @TcpCriteriaId([id])
  10. @TcpBuffer()
  11. @TcpSocket()
  12. @TcpInjectSocket()

Installation

npm install tcp-controller --save

How to use?

The first thing to do to create your gateway and interact with a server that supports flow-oriented TCP/IP or message flow is to create a class and annotate it with @TcpController, then add at least one method of message processing annotated by @TcpMessage. After creating the class, you need to call createServer to start the lifecycle of your gateway if it is a server or clientConnect if the gateway is a TCP/IP client.

In TCP Controller, the life cycle of a TCP connection is regulated by 3 decorators, they are: @TcpClientConnected, @TcpClientDisconnected, @TcpError. Whenever the connection is successfully established, the method decorated with @TcpClientConnected is invoked, just as when the connection is closed the method decorated with @TcpClientDisconnected is invoked. In TCP Controller there are 2 types of exceptions, those caused by network failures and those caused by application code failures. The method decorated with @TcpError catches TCP connection errors. There is currently no equivalent for exceptions caused by application crashes.

To send a reply for each incoming message you can use the send function which is declared in the TcpGateway class. Create your Tcp Controller class and inherit from the TcpGateway class. You can call the send function more than once in an incoming message.

For a more detailed overview see the example of integration below using a Gateway to receive messages. Here is a TCP server sample:

import { Socket } from "net";
import {
  createServer,
  NativeSocket,
  TcpGateway,
  TcpBuffer,
  TcpClientConnected,
  TcpClientDisconnected,
  TcpController,
  TcpCriteria,
  TcpCriteriaId,
  TcpError,
  TcpListening,
  TcpMessage,
  TcpMessageStream,
  TcpSocket
} from "tcp-controller";

@TcpController("0.0.0.0", 1337)
@TcpMessageStream()
class TcpServer extends TcpGateway {

  @TcpClientConnected()
  onConnect(@TcpSocket() sock: NativeSocket) {
    console.log("[Server] Connected client from " + sock.remoteAddress);
    this.send(sock, Buffer.from("Server to client!"));
    this.send(sock, Buffer.from("5erver to client!"));
  }

  @TcpClientDisconnected()
  onDisconnect(hadError: boolean) {
    console.log("[Server] Disconnected client with error? " + hadError);
  }

  @TcpError()
  onError(error: Error) {
    console.log("[Server] Error happened: " + error.message);
  }

  @TcpListening()
  onListening() {
    console.log("[Server] Server up.");
  }

  @TcpCriteria("message")
  @TcpMessage()
  onMessage(@TcpBuffer() buffer: Buffer, @TcpSocket() sock: NativeSocket) {
    console.log("[Server] Message received from " + sock.remoteAddress + ".\nMessage: " + buffer.toString());
  }

  @TcpCriteriaId("message")
  isMessage(@TcpBuffer() buffer: Buffer) {
    return buffer.toString().includes("Client to server!");
  }

}

createServer(TcpServer);

And here is a Client TCP Gateway:

import { Socket } from "net";
import {
  clientConnect,
  NativeSocket,
  TcpGateway,
  TcpBuffer,
  TcpClientConnected,
  TcpClientDisconnected,
  TcpController,
  TcpCriteria,
  TcpCriteriaId,
  TcpError,
  TcpMessage,
  TcpMessageStream,
  TcpSocket
} from "tcp-controller";

@TcpController("0.0.0.0", 1337)
@TcpMessageStream()
class TcpClient extends TcpGateway {

  @TcpClientConnected()
  onConnect(@TcpSocket() sock: NativeSocket) {
    console.log("[Client] Connected to server " + sock.remoteAddress);
    this.send(sock, Buffer.from("Client to server!"));
    this.send(sock, Buffer.from("Client to 5erver!"));
  }

  @TcpClientDisconnected()
  onDisconnect(hadError: boolean) {
    console.log("[Client] Disconnected with error? " + hadError);
  }

  @TcpError()
  onError(error: Error) {
    console.log("[Client] Error happened: " + error.message);
  }

  @TcpCriteria("message")
  @TcpMessage()
  onMessage(@TcpBuffer() buffer: Buffer, @TcpSocket() sock: NativeSocket) {
    console.log("[Client] Message received from " + sock.remoteAddress + ".\nMessage: " + buffer.toString());
  }

  @TcpCriteriaId("message")
  isMessage(@TcpBuffer() buffer: Buffer) {
    return buffer.toString().includes("Server to client!");
  }

}

clientConnect(TcpClient);

Metadata

Muryllo Pimenta de Oliveira – [email protected]

Distributed under MIT license. See LICENSE for more informations.

Contributing

  1. Create a fork (https://github.com/MurylloEx/tcp-controller/fork)
  2. Create a feature branch (git checkout -b feature/fooBar)
  3. Commit your changes (git commit -am 'Add some fooBar')
  4. Send a push of your commit (git push origin feature/fooBar)
  5. Open a new Pull Request