@@ -54,9 +54,8 @@ Sender.prototype.close = function(code, data, mask, cb) {
54
54
if ( dataBuffer . length > 2 ) dataBuffer . write ( data , 2 ) ;
55
55
56
56
var self = this ;
57
- this . messageHandlers . push ( function ( callback ) {
57
+ this . messageHandlers . push ( function ( ) {
58
58
self . frameAndSend ( 0x8 , dataBuffer , true , mask ) ;
59
- callback ( ) ;
60
59
if ( typeof cb == 'function' ) cb ( ) ;
61
60
} ) ;
62
61
this . flush ( ) ;
@@ -71,9 +70,8 @@ Sender.prototype.close = function(code, data, mask, cb) {
71
70
Sender . prototype . ping = function ( data , options ) {
72
71
var mask = options && options . mask ;
73
72
var self = this ;
74
- this . messageHandlers . push ( function ( callback ) {
73
+ this . messageHandlers . push ( function ( ) {
75
74
self . frameAndSend ( 0x9 , data || '' , true , mask ) ;
76
- callback ( ) ;
77
75
} ) ;
78
76
this . flush ( ) ;
79
77
} ;
@@ -87,9 +85,8 @@ Sender.prototype.ping = function(data, options) {
87
85
Sender . prototype . pong = function ( data , options ) {
88
86
var mask = options && options . mask ;
89
87
var self = this ;
90
- this . messageHandlers . push ( function ( callback ) {
88
+ this . messageHandlers . push ( function ( ) {
91
89
self . frameAndSend ( 0xa , data || '' , true , mask ) ;
92
- callback ( ) ;
93
90
} ) ;
94
91
this . flush ( ) ;
95
92
} ;
@@ -117,15 +114,22 @@ Sender.prototype.send = function(data, options, cb) {
117
114
var compressFragment = this . compress ;
118
115
119
116
var self = this ;
120
- this . messageHandlers . push ( function ( callback ) {
117
+ this . messageHandlers . push ( function ( ) {
118
+ if ( ! data || ! compressFragment ) {
119
+ self . frameAndSend ( opcode , data , finalFragment , mask , compress , cb ) ;
120
+ return ;
121
+ }
122
+
123
+ self . processing = true ;
121
124
self . applyExtensions ( data , finalFragment , compressFragment , function ( err , data ) {
122
125
if ( err ) {
123
126
if ( typeof cb == 'function' ) cb ( err ) ;
124
127
else self . emit ( 'error' , err ) ;
125
128
return ;
126
129
}
127
130
self . frameAndSend ( opcode , data , finalFragment , mask , compress , cb ) ;
128
- callback ( ) ;
131
+ self . processing = false ;
132
+ self . flush ( ) ;
129
133
} ) ;
130
134
} ) ;
131
135
this . flush ( ) ;
@@ -257,21 +261,9 @@ Sender.prototype.frameAndSend = function(opcode, data, finalFragment, maskData,
257
261
*/
258
262
259
263
Sender . prototype . flush = function ( ) {
260
- if ( this . processing ) return ;
261
-
262
- var handler = this . messageHandlers . shift ( ) ;
263
- if ( ! handler ) return ;
264
-
265
- this . processing = true ;
266
-
267
- var self = this ;
268
-
269
- handler ( function ( ) {
270
- process . nextTick ( function ( ) {
271
- self . processing = false ;
272
- self . flush ( ) ;
273
- } ) ;
274
- } ) ;
264
+ while ( ! this . processing && this . messageHandlers . length ) {
265
+ this . messageHandlers . shift ( ) ( ) ;
266
+ }
275
267
} ;
276
268
277
269
/**
@@ -281,14 +273,10 @@ Sender.prototype.flush = function() {
281
273
*/
282
274
283
275
Sender . prototype . applyExtensions = function ( data , fin , compress , callback ) {
284
- if ( compress && data ) {
285
- if ( ( data . buffer || data ) instanceof ArrayBuffer ) {
286
- data = getArrayBuffer ( data ) ;
287
- }
288
- this . extensions [ PerMessageDeflate . extensionName ] . compress ( data , fin , callback ) ;
289
- } else {
290
- callback ( null , data ) ;
276
+ if ( ( data . buffer || data ) instanceof ArrayBuffer ) {
277
+ data = getArrayBuffer ( data ) ;
291
278
}
279
+ this . extensions [ PerMessageDeflate . extensionName ] . compress ( data , fin , callback ) ;
292
280
} ;
293
281
294
282
module . exports = Sender ;
0 commit comments