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

@qtk/tcp-framework

v3.0.1

Published

qtk微服务框架体系一环,为最底层的异步tcp请求框架。框架内部维护连接心跳,默认每20秒发送一个心跳包,30秒连接无响应超时。 **请求体/响应体必须为Buffer形式,框架不解析请求体/响应体内容,故数据协议开发者可以自行定义**。

Downloads

41

Readme

qtk-tcp-framework

qtk微服务框架体系一环,为最底层的异步tcp请求框架。框架内部维护连接心跳,默认每20秒发送一个心跳包,30秒连接无响应超时。 请求体/响应体必须为Buffer形式,框架不解析请求体/响应体内容,故数据协议开发者可以自行定义

Server初始化

  • host: 监听ip,默认localhost
  • port: 监听端口
  • heartbeat: 心跳包间隔(单位秒),默认20
  • timeout: 连接无响应超时(单位秒),默认30。(服务端不响应客户端请求情况不算连接超时,此时还有心跳包,故不会触发超时)
const server = new Server({ port: 8212 });

Client初始化

  • host: 服务端ip,默认localhost
  • port: 服务端端口
  • timeout: 连接无响应超时(单位秒),默认30。(服务端不响应客户端请求情况不算连接超时,此时还有心跳包,故不会触发超时)
const client = new Client({ port: 8212 });

客户端发送请求

  • uuid: 36位uuid(含-), 每次请求的唯一标识
  • data: 请求体buffer
client.send({ uuid: uuid(), data: Buffer.from('echo') });

服务端接受请求

服务端监听data事件,回调函数包含两个参数:

  • socket: 客户端socket对象
  • 请求体信息:
    • uuid: 客户端某次请求的uuid
    • data: 请求体buffer
server.on('data', (socket, {uuid, data}) => {

})

服务端响应请求

服务端监听data事件,收到请求后,调用send方法响应本次客户端请求。send方法包含两个参数:

  • socket: 客户端socket对象
  • 响应体信息:
    • uuid: 响应客户端某次请求的uuid
    • data: 响应体buffer
server.on('data', (socket, {uuid, data}) => {
    server.send(socket, {uuid, data}); //原封不动返回data
})

客户端接受服务端响应

客户端监听data事件,回调函数包含一个参数:

  • 响应体信息:
    • uuid: 客户端某次请求的uuid
    • data: 响应体buffer
client.on('data', ({uuid, data}) => {

})

系统事件

Server

  • started: 服务端启动
  • stopped: 服务端停止
  • connected: 新的客户端连接,参数: (socket)
  • closed: 客户端断开,参数: (socket)
  • exception: 系统出错,参数: (socket, error)
  • data: 客户端请求, 参数: (socket, {uuid, data})

Client

  • connected: 连接服务端成功
  • closed: 连接关闭
  • exception: 系统出错,参数: (error)
  • data: 服务端响应, 参数: ({uuid, data})

Demo

//server.js
const Server = require('@qtk/tcp-framework').Server;
const server = new Server({ port: 8212 });

server.on('data', (socket, {uuid, data}) => {
    switch(data.toString('utf8')) {
        case "echo":
            server.send(socket, {uuid, data});
            break;
        case "delayed_echo":
            setTimeout(() => {
                server.send(socket, {uuid, data});
            }, 3000);
            break;
        default:
            break;
    }
});

server.start();

//client.js
const Client = require('@qtk/tcp-framework').Client;
const uuid = require('uuid').v1;
const port = 8212;

client.on('data', ({uuid, data}) => {
    console.log(`got response, uuid: ${uuid}, data: ${data.toString('utf8')}`);
});

client.send({ uuid: uuid(), data: Buffer.from('echo') });

备注

  • 3.0版本与之前的版本协议不兼容,请勿混合使用
  • 3.0版本比2.0版性能提升13%