@@ -28,11 +28,13 @@ function WebSocket (req) {
28
28
this . socket . on ( 'message' , this . onData . bind ( this ) ) ;
29
29
this . socket . once ( 'close' , this . onClose . bind ( this ) ) ;
30
30
this . socket . on ( 'error' , this . onError . bind ( this ) ) ;
31
- this . socket . on ( 'headers' , function ( headers ) {
32
- self . emit ( 'headers' , headers ) ;
33
- } ) ;
31
+ this . socket . on ( 'headers' , onHeaders ) ;
34
32
this . writable = true ;
35
33
this . perMessageDeflate = null ;
34
+
35
+ function onHeaders ( headers ) {
36
+ self . emit ( 'headers' , headers ) ;
37
+ }
36
38
}
37
39
38
40
/**
@@ -86,31 +88,37 @@ WebSocket.prototype.onData = function (data) {
86
88
87
89
WebSocket . prototype . send = function ( packets ) {
88
90
var self = this ;
89
- packets . forEach ( function ( packet ) {
90
- parser . encodePacket ( packet , self . supportsBinary , function ( data ) {
91
- debug ( 'writing "%s"' , data ) ;
92
-
93
- // always creates a new object since ws modifies it
94
- var opts = { } ;
95
- if ( packet . options ) {
96
- opts . compress = packet . options . compress ;
97
- }
98
91
99
- if ( self . perMessageDeflate ) {
100
- var len = 'string' === typeof data ? Buffer . byteLength ( data ) : data . length ;
101
- if ( len < self . perMessageDeflate . threshold ) {
102
- opts . compress = false ;
103
- }
92
+ for ( var i = 0 ; i < packets . length ; i ++ ) {
93
+ var packet = packets [ i ] ;
94
+ parser . encodePacket ( packet , self . supportsBinary , send ) ;
95
+ }
96
+
97
+ function send ( data ) {
98
+ debug ( 'writing "%s"' , data ) ;
99
+
100
+ // always creates a new object since ws modifies it
101
+ var opts = { } ;
102
+ if ( packet . options ) {
103
+ opts . compress = packet . options . compress ;
104
+ }
105
+
106
+ if ( self . perMessageDeflate ) {
107
+ var len = 'string' === typeof data ? Buffer . byteLength ( data ) : data . length ;
108
+ if ( len < self . perMessageDeflate . threshold ) {
109
+ opts . compress = false ;
104
110
}
111
+ }
112
+
113
+ self . writable = false ;
114
+ self . socket . send ( data , opts , onEnd ) ;
115
+ }
105
116
106
- self . writable = false ;
107
- self . socket . send ( data , opts , function ( err ) {
108
- if ( err ) return self . onError ( 'write error' , err . stack ) ;
109
- self . writable = true ;
110
- self . emit ( 'drain' ) ;
111
- } ) ;
112
- } ) ;
113
- } ) ;
117
+ function onEnd ( err ) {
118
+ if ( err ) return self . onError ( 'write error' , err . stack ) ;
119
+ self . writable = true ;
120
+ self . emit ( 'drain' ) ;
121
+ }
114
122
} ;
115
123
116
124
/**
0 commit comments