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

socketx

v1.1.0

Published

socket pool

Downloads

15

Readme

socketx

NPM version node version build status Test coverage License

socketx用于在Node中建立socket连接,使用简单,且支持设置http代理及自定义建立连接的方式。

安装

npm i --save socketx

使用

服务器代码:

// server.js
const net = require('net');

const noop = _ => _;
const server = net.createServer((socket) => {
    socket.on('error', noop);
    socket.on('data', (data) => {
      socket.write(`response: ${data}`);
    });
  });
server.listen(9999, () => {
	console.log(`server listening on ${server.address().port}.`);
});

普通客户端代码:

const { connect } = require('socketx');

(async () => {
	const client = await connect({
		host: '127.0.0.1',
		port: 9999,
	});
	client.on('data', (data) => {
		console.log(`${data}`);
	});
	client.on('error', (e) => console.error(e));
	setInterval(() => {
		client.write('test');
	}, 3000);
})();

连接池代码:

连接池的缓存key是通过 connect(options) 里面参数 host:port[:name]path[:name] 生成,其中 name 默认为空;可以通过name控制相同 host:portpath 长连接的缓存个数

const { Pool } = require('socketx');

const pool = new Pool();
(async () => {
	const client = await pool.connect({
		host: '127.0.0.1',
		port: 9999,
	});
	client.on('data', (data) => {
		console.log(`${data}`);
	});
	client.on('error', (e) => console.error(e));
	setInterval(() => {
		client.write('test');
	}, 3000);
})();

通过代理到whistle

const { connect } = require('socketx');

(async () => {
	const proxy = {
		host: '127.0.0.1',
		port: 8899,
	};
	const client = await connect({
		host: '127.0.0.1',
		port: 9999,
		proxy,
	});
	client.on('data', (data) => {
		console.log(`${data}`);
	});
	client.on('error', (e) => console.error(e));
	setInterval(() => {
		client.write('test');
	}, 3000);
})();

API

const { connect, Pool } = require('socketx');

connect(options)

options:

  • host: 服务器的ip或域名
  • port: 服务器的端口
  • proxy: 设置http代理
    • host: 代理服务器ip或域名
    • port: 代理服务器端口
    • headers: 自定义代理请求头
  • createConnection(options): 自定义建立连接方式,options为connect(options),返回 Promisesocket 对象
  • connectTimeout: 可选,socket连接超时毫秒数,默认为3000ms,如果值为非正数,表示不设置超时
  • idleTimeout:可选,设置socket的空闲超时毫秒数,socket在idleTimeout时间内没有传输数据将自动销毁,默认为0,不设置idleTimeout时间,如果值为非正数,表示不设置超时
  • path: 同socket

其中 host:portpath 至少要存在一个。

new Pool(options)

options:

  • proxy: 设置http代理,pool.connect 里面的 proxy 参数优先级高于该配置
    • host: 代理服务器ip或域名
    • port: 代理服务器端口
    • headers: 自定义代理请求头
  • connectTimeout: 可选,设置连接池里面默认socket连接超时毫秒数,默认为3000ms,如果值为非正数,表示不设置超时,该设置可以通过 pool.connect(opts)opts.connectTimeout 修改
  • idleTimeout:可选,设置连接池里面默认socket的空闲超时毫秒数,socket在idleTimeout时间内没有传输数据将自动销毁,默认为0,不设置idleTimeout时间,如果值为非正数,表示不设置超时,该设置可以通过 pool.connect(opts)opts.idleTimeout 修改
  • createConnection: 可选,自定义建立连接方式,该设置可以通过 pool.connect(opts)opts.createConnection 修改

pool.connect(options)

options:

  • host: 服务器的ip或域名
  • port: 服务器的端口
  • proxy: 设置http代理,优先级高于构造函数的 proxy 参数
    • host: 代理服务器ip或域名
    • port: 代理服务器端口
    • headers: 自定义代理请求头
  • name: 可选,连接名称,主要用于协助设置缓存的key
  • createConnection(options): 自定义建立连接方式,options为connect(options),返回 Promisesocket 对象
  • connectTimeout: 可选,socket连接超时毫秒数,默认为3000ms,如果值为非正数,表示不设置超时
  • idleTimeout:可选,设置socket的空闲超时毫秒数,socket在idleTimeout时间内没有传输数据将自动销毁,默认为0,不设置idleTimeout时间,如果值为非正数,表示不设置超时
  • path: 同socket

其中 host:portpath 至少要存在一个,如果相同的 host:port 要缓存池里面建立多条连接可以采用设置 name 的方式区分,因为缓存的key是通过按优先顺序 host:port[:name]path[:name] 生成,其中 name 默认为空。