1
1
import { Transport } from "../transport" ;
2
2
import debugModule from "debug" ;
3
+ import type { Packet , RawData } from "engine.io-parser" ;
3
4
4
5
const debug = debugModule ( "engine:ws" ) ;
5
6
@@ -45,52 +46,27 @@ export class WebSocket extends Transport {
45
46
return true ;
46
47
}
47
48
48
- /**
49
- * Writes a packet payload.
50
- *
51
- * @param {Array } packets
52
- * @api private
53
- */
54
- send ( packets ) {
49
+ send ( packets : Packet [ ] ) {
55
50
this . writable = false ;
56
51
57
52
for ( let i = 0 ; i < packets . length ; i ++ ) {
58
53
const packet = packets [ i ] ;
59
54
const isLast = i + 1 === packets . length ;
60
55
61
- // always creates a new object since ws modifies it
62
- const opts : { compress ?: boolean } = { } ;
63
- if ( packet . options ) {
64
- opts . compress = packet . options . compress ;
65
- }
66
-
67
- const onSent = ( err ) => {
68
- if ( err ) {
69
- return this . onError ( "write error" , err . stack ) ;
70
- } else if ( isLast ) {
71
- this . writable = true ;
72
- this . emit ( "drain" ) ;
73
- }
74
- } ;
75
-
76
- const send = ( data ) => {
77
- if ( this . perMessageDeflate ) {
78
- const len =
79
- "string" === typeof data ? Buffer . byteLength ( data ) : data . length ;
80
- if ( len < this . perMessageDeflate . threshold ) {
81
- opts . compress = false ;
82
- }
83
- }
84
- debug ( 'writing "%s"' , data ) ;
85
- this . socket . send ( data , opts , onSent ) ;
86
- } ;
87
-
88
56
if ( this . _canSendPreEncodedFrame ( packet ) ) {
89
57
// the WebSocket frame was computed with WebSocket.Sender.frame()
90
58
// see https://github.com/websockets/ws/issues/617#issuecomment-283002469
91
- this . socket . _sender . sendFrame ( packet . options . wsPreEncodedFrame , onSent ) ;
59
+ this . socket . _sender . sendFrame (
60
+ // @ts -ignore
61
+ packet . options . wsPreEncodedFrame ,
62
+ isLast ? this . _onSentLast : this . _onSent
63
+ ) ;
92
64
} else {
93
- this . parser . encodePacket ( packet , this . supportsBinary , send ) ;
65
+ this . parser . encodePacket (
66
+ packet ,
67
+ this . supportsBinary ,
68
+ isLast ? this . _doSendLast : this . _doSend
69
+ ) ;
94
70
}
95
71
}
96
72
}
@@ -100,20 +76,39 @@ export class WebSocket extends Transport {
100
76
* @param packet
101
77
* @private
102
78
*/
103
- private _canSendPreEncodedFrame ( packet ) {
79
+ private _canSendPreEncodedFrame ( packet : Packet ) {
104
80
return (
105
81
! this . perMessageDeflate &&
106
82
typeof this . socket ?. _sender ?. sendFrame === "function" &&
83
+ // @ts -ignore
107
84
packet . options ?. wsPreEncodedFrame !== undefined
108
85
) ;
109
86
}
110
87
111
- /**
112
- * Closes the transport.
113
- *
114
- * @api private
115
- */
116
- doClose ( fn ) {
88
+ private _doSend = ( data : RawData ) => {
89
+ this . socket . send ( data , this . _onSent ) ;
90
+ } ;
91
+
92
+ private _doSendLast = ( data : RawData ) => {
93
+ this . socket . send ( data , this . _onSentLast ) ;
94
+ } ;
95
+
96
+ private _onSent = ( err ?: Error ) => {
97
+ if ( err ) {
98
+ this . onError ( "write error" , err . stack ) ;
99
+ }
100
+ } ;
101
+
102
+ private _onSentLast = ( err ?: Error ) => {
103
+ if ( err ) {
104
+ this . onError ( "write error" , err . stack ) ;
105
+ } else {
106
+ this . writable = true ;
107
+ this . emit ( "drain" ) ;
108
+ }
109
+ } ;
110
+
111
+ doClose ( fn ?: ( ) => void ) {
117
112
debug ( "closing" ) ;
118
113
this . socket . close ( ) ;
119
114
fn && fn ( ) ;
0 commit comments