เนื่องจาก libuv v1.35.0 (โหนด
v13.12.0&v12.17.0) ทั้งTCP_KEEPINTVLและTCP_KEEPCNTมีค่าที่คาดการณ์ได้บ้างแพคเกจนี้ช่วยให้คุณปรับค่าเหล่านั้นต่อซ็อกเก็ตแต่ละตัว ตรวจสอบเอกสารโหนดล่าสุดสำหรับซ็อกเก็ต SetetKeepaliveEnable หากค่าเริ่มต้นดีพอสำหรับคุณคุณไม่จำเป็นต้องใช้แพ็คเกจนี้
TCP_KEEPCNT TCP_KEEPINTVL SO_KEEPALIVE Socket Setters และ getters สำหรับโหนดโดยใช้ FFI
ทดสอบบน linux & osx (ทั้ง amd64 และ arm64 ) ควรทำงานกับ freebsd และอื่น ๆ การติดตั้งบน win32 แต่วิธีการไม่ได้ (ยินดีต้อนรับดึงคำขอ)
นอกจากนี้ยังมีการสนับสนุนสำหรับการรับ & การตั้งค่าตัวเลือก 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-keepaliveคุณสามารถค้นหาเอกสารอ้างอิง API เต็มรูปแบบ (JSDOC) ที่เผยแพร่ในหน้า GitHub ของเรา
โครงการรวมไฟล์คำจำกัดความ typeScript ( index.d.ts ) ซึ่งให้ภาพรวมของ API ที่เปิดเผย
เอกสารได้รับการสร้างขึ้นจากความคิดเห็น 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 เพิ่มกฎเพื่อวางแพ็กเก็ต tcp ทั้งหมดบนห่วง INPUT ไปยังพอร์ต 1337
iptables -I INPUT -m tcp -p tcp --dport 1337 -j DROP หากคุณกำลังตรวจสอบแพ็คเก็ตบน loopback ด้วย tcp.srcport == 1337 || tcp.dstport == 1337 ตัวกรองใน wireshark คุณจะเห็นผลลัพธ์ต่อไปนี้:
มีความสุข!
ข้อมูลเพิ่มเติมเกี่ยวกับ SO_KEEPALIVE ที่นี่: tcp keepalive C code ตัวอย่างรหัสที่นี่: ตัวอย่าง
หมายเหตุ: สำหรับวิธีการเหล่านี้ในการทำงานคุณต้องเปิดใช้งาน SO_KEEPALIVE และตั้งค่าตัวเลือก TCP_KEEPIDLE สำหรับซ็อกเก็ตโดยใช้ Net.Socket -s ในการสร้าง socket.setKeepAlive([enable][, initialDelay]) !
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
ดูการสนับสนุน. md
ขอบคุณไปที่คนที่ยอดเยี่ยมเหล่านี้ (คีย์อีโมจิ):
George Hertz | Alba Mendez | เปาโลคาสโตร | Jacob Jewell | rmutharaju | Rafael Borges | คาลวิน |
GGSUBS | Mario Kozjak | Lukas Knuth | อีวาน | Otávio Jacobi |
โครงการนี้เป็นไปตามข้อกำหนดทั้งหมดของผู้เข้าร่วม การมีส่วนร่วมทุกชนิดยินดีต้อนรับ!