libuv v1.35.0(ノード
v13.12.0&v12.17.0)以来、TCP_KEEPINTVLとTCP_KEEPCNTの両方がやや予測可能な値を持っています。 Socket.SetKeePaliveEnableの最新ノードドキュメントを確認してください。デフォルト値が十分である場合は、このパッケージを使用する必要はありません。
FFIを使用したノード用のSO_KEEPALIVEソケットオプションセッターとso_keepaliveソケットオプションセッターの欠落( TCP_KEEPINTVLおよびTCP_KEEPCNT )。
linux & osx ( amd64とarm64の両方)でテストされ、 freebsdなどで動作するはずです。 win32にインストールしますが、メソッドはno-opsです(プルリクエストを歓迎します)。
また、 TCP_USER_TIMEOUT ( linuxおよびosxのみ)オプションを取得および設定するサポートもあります。これは、Keep-Aliveに密接に関連しています。
| プラットフォーム | TCP_KEEPINTVL | tcp_keepcnt | tcp_user_timeout |
|---|---|---|---|
linux | |||
osx | ( TCP_RXT_CONNDROPTIME ) | ||
freebsd | |||
win32 |
伝説:
npm install --save net-keepaliveGitHubページで公開されている完全なAPIリファレンスドキュメント(JSDOC)を見つけることができます。
このプロジェクトには、APIの公開の概要を示すTypeScript定義ファイル( index.d.ts )が含まれています。
ドキュメントはJSDOCのコメントから生成されます。プルリクエストを送信することで、自由に改善してください。
const Net = require ( 'net' ) ,
NetKeepAlive = require ( 'net-keepalive' )
// or
import * as Net from 'net'
import * as NetKeepAlive from 'net-keepalive'
// Create a TCP Server
const srv = Net . createServer ( ( s ) => {
console . log ( 'Connected %j' , s . address ( ) )
// Doesn't matter what it does
s . pipe ( s )
} )
// Start on some port
srv . listen ( 1337 , ( ) => {
console . log ( 'Listening on %j' , srv . address ( ) )
} )
// Connect to that server
const s = Net . createConnection ( { port : 1337 } , ( ) => {
console . log ( 'Connected to %j' , s . address ( ) )
//IMPORTANT: KeepAlive must be enabled for this to work
s . setKeepAlive ( true , 1000 )
// Set TCP_KEEPINTVL for this specific socket
NetKeepAlive . setKeepAliveInterval ( s , 1000 )
// Get TCP_KEEPINTVL for this specific socket
NetKeepAlive . getKeepAliveInterval ( s ) // 1000
// Set TCP_KEEPCNT for this specific socket
NetKeepAlive . setKeepAliveProbes ( s , 1 )
// Get TCP_KEEPCNT for this specific socket
NetKeepAlive . getKeepAliveProbes ( s ) // 1
} ) iptablesを使用してルールを追加して、 INPUTチェーン上のすべてのtcpパケットをポート1337にドロップします。
iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP tcp.srcport == 1337 || tcp.dstport == 1337でloopbackでパケットを監視している場合|| tcp.srcport == 1337 || tcp.dstport == 1337 wiresharkのフィルター。次の出力が表示されます。
楽しむ!
SO_KEEPALIVE詳細については、こちらのTCP Keepalive Howto Cコードの例はこちら:例
注:これらのメソッドは、 SO_KEEPALIVEを有効にし、 Net.Socket -socket- socket.setKeepAlive([enable][, initialDelay])を使用してソケットのTCP_KEEPIDLEオプションを設定する必要があります。
TCP_KEEPIDLE (since Linux 2.4) The time (in seconds) the connection needs to remain idle before TCP starts sending keepalive probes, if the socket option SO_KEEPALIVE has been set on this socket. This option should not be used in code intended to be portable.
const NetKeepAlive = require ( 'net-keepalive' )
// or
import * as NetKeepAlive from 'net-keepalive'
// .....
const enable = true // enable SO_KEEPALIVE
const initialDuration = 1000 // start probing after 1 second of inactivity
socket . setKeepAlive ( enable , initialDuration ) // sets SO_KEEPALIVE and TCP_KEEPIDLE
const probeInterval = 1000 // after initialDuration send probes every 1 second
NetKeepAlive . setKeepAliveInterval ( socket , probeInterval ) //sets TCP_KEEPINTVL
const maxProbesBeforeFail = 10 // after 10 failed probes connection will be dropped
NetKeepAlive . setKeepAliveProbes ( socket , maxProbesBeforeFail ) // sets TCP_KEEPCNT
// .... code_of_conduct.mdを参照してください
Convributing.mdを参照してください
これらの素晴らしい人々に感謝します(絵文字キー):
ジョージ・ヘルツ | アルバ・メンデス | パウロカストロ | ジェイコブ・ジュウェル | rmutharaju | ラファエル・ボルジェス | カルバン |
ggsubs | マリオ・コジャック | ルーカス・クヌース | イヴァン | OtávioJacobi |
このプロジェクトは、全委員会の仕様に従います。あらゆる種類の貢献を歓迎します!