@@ -73,13 +73,15 @@ function WebSocketServer(options, callback) {
73
73
this . _server . _webSocketPaths [ options . value . path ] = 1 ;
74
74
}
75
75
}
76
- if ( this . _server ) this . _server . once ( 'listening' , function ( ) { self . emit ( 'listening' ) ; } ) ;
76
+ if ( this . _server ) {
77
+ this . _onceServerListening = function ( ) { self . emit ( 'listening' ) ; } ;
78
+ this . _server . once ( 'listening' , this . _onceServerListening ) ;
79
+ }
77
80
78
81
if ( typeof this . _server != 'undefined' ) {
79
- this . _server . on ( 'error' , function ( error ) {
80
- self . emit ( 'error' , error )
81
- } ) ;
82
- this . _server . on ( 'upgrade' , function ( req , socket , upgradeHead ) {
82
+ this . _onServerError = function ( error ) { self . emit ( 'error' , error ) } ;
83
+ this . _server . on ( 'error' , this . _onServerError ) ;
84
+ this . _onServerUpgrade = function ( req , socket , upgradeHead ) {
83
85
//copy upgradeHead to avoid retention of large slab buffers used in node core
84
86
var head = new Buffer ( upgradeHead . length ) ;
85
87
upgradeHead . copy ( head ) ;
@@ -88,7 +90,8 @@ function WebSocketServer(options, callback) {
88
90
self . emit ( 'connection' + req . url , client ) ;
89
91
self . emit ( 'connection' , client ) ;
90
92
} ) ;
91
- } ) ;
93
+ } ;
94
+ this . _server . on ( 'upgrade' , this . _onServerUpgrade ) ;
92
95
}
93
96
94
97
this . options = options . value ;
@@ -135,6 +138,11 @@ WebSocketServer.prototype.close = function(callback) {
135
138
}
136
139
}
137
140
finally {
141
+ if ( this . _server ) {
142
+ this . _server . removeListener ( 'listening' , this . _onceServerListening ) ;
143
+ this . _server . removeListener ( 'error' , this . _onServerError ) ;
144
+ this . _server . removeListener ( 'upgrade' , this . _onServerUpgrade ) ;
145
+ }
138
146
delete this . _server ;
139
147
}
140
148
if ( callback )
0 commit comments