libuv v1.35.0 (노드
v13.12.0&v12.17.0)이므로TCP_KEEPINTVL과TCP_KEEPCNT는 다소 예측 가능한 값을 가지고 있기 때문에이 패키지는 각 소켓마다 해당 값을 조정할 수 있지만 FFI 오버 헤드를 다루는 데 드는 비용으로 해당 값을 조정할 수 있습니다. Socket.setKeepaliveEnable의 최신 노드 문서를 확인하십시오. 기본값이 충분하면이 패키지를 사용할 필요가 없습니다.
누락 된 ( TCP_KEEPINTVL 및 TCP_KEEPCNT ) SO_KEEPALIVE 소켓 옵션 세터 및 FFI를 사용한 노드 용 getters.
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 Deferents File ( 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를 사용하여 loopback 에서 패킷을 모니터링하는 경우 tcp.srcport == 1337 || tcp.dstport == 1337 wireshark 의 필터. 다음 출력이 표시됩니다.
재미있게 보내세요!
SO_KEEPALIVE 에 대한 자세한 정보 : TCP KeepAlive Howto C 코드 예제 : 예제 : 예제
참고 : 이러한 방법이 작동하려면 SO_KEEPALIVE 활성화하고 Net.Socket -S Method 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를 참조하십시오
Contributing.md를 참조하십시오
이 멋진 사람들에게 감사합니다 (이모티콘 키) :
조지 헤르츠 | 알바 멘데즈 | 파울로 카스트로 | 야곱 보석 | Rmutharaju | 라파엘 보스 | 칼뱅 |
ggsubs | 마리오 코즈 자크 | Lukas Knuth | 이반 | 오타 비오 자코비 |
이 프로젝트는 All-Contritors 사양을 따릅니다. 모든 종류의 공헌을 환영합니다!