@@ -18,6 +18,7 @@ const { isBlob } = require('./validation');
1818
1919const {
2020 BINARY_TYPES ,
21+ CLOSE_TIMEOUT ,
2122 EMPTY_BUFFER ,
2223 GUID ,
2324 kForOnEventAttribute,
@@ -32,7 +33,6 @@ const {
3233const { format, parse } = require ( './extension' ) ;
3334const { toBuffer } = require ( './buffer-util' ) ;
3435
35- const closeTimeout = 30 * 1000 ;
3636const kAborted = Symbol ( 'kAborted' ) ;
3737const protocolVersions = [ 8 , 13 ] ;
3838const readyStates = [ 'CONNECTING' , 'OPEN' , 'CLOSING' , 'CLOSED' ] ;
@@ -88,6 +88,7 @@ class WebSocket extends EventEmitter {
8888 initAsClient ( this , address , protocols , options ) ;
8989 } else {
9090 this . _autoPong = options . autoPong ;
91+ this . _closeTimeout = options . closeTimeout ;
9192 this . _isServer = true ;
9293 }
9394 }
@@ -629,6 +630,8 @@ module.exports = WebSocket;
629630 * times in the same tick
630631 * @param {Boolean } [options.autoPong=true] Specifies whether or not to
631632 * automatically send a pong in response to a ping
633+ * @param {Number } [options.closeTimeout=30000] Duration in milliseconds to wait
634+ * for the closing handshake to finish after `websocket.close()` is called
632635 * @param {Function } [options.finishRequest] A function which can be used to
633636 * customize the headers of each http request before it is sent
634637 * @param {Boolean } [options.followRedirects=false] Whether or not to follow
@@ -655,6 +658,7 @@ function initAsClient(websocket, address, protocols, options) {
655658 const opts = {
656659 allowSynchronousEvents : true ,
657660 autoPong : true ,
661+ closeTimeout : CLOSE_TIMEOUT ,
658662 protocolVersion : protocolVersions [ 1 ] ,
659663 maxPayload : 100 * 1024 * 1024 ,
660664 skipUTF8Validation : false ,
@@ -673,6 +677,7 @@ function initAsClient(websocket, address, protocols, options) {
673677 } ;
674678
675679 websocket . _autoPong = opts . autoPong ;
680+ websocket . _closeTimeout = opts . closeTimeout ;
676681
677682 if ( ! protocolVersions . includes ( opts . protocolVersion ) ) {
678683 throw new RangeError (
@@ -1290,7 +1295,7 @@ function senderOnError(err) {
12901295function setCloseTimer ( websocket ) {
12911296 websocket . _closeTimer = setTimeout (
12921297 websocket . _socket . destroy . bind ( websocket . _socket ) ,
1293- closeTimeout
1298+ websocket . _closeTimeout
12941299 ) ;
12951300}
12961301
0 commit comments