@@ -42,7 +42,6 @@ class Socket extends Emitter {
42
42
opts . port = this . secure ? "443" : "80" ;
43
43
}
44
44
45
- this . agent = opts . agent || false ;
46
45
this . hostname =
47
46
opts . hostname ||
48
47
( typeof location !== "undefined" ? location . hostname : "localhost" ) ;
@@ -53,62 +52,42 @@ class Socket extends Emitter {
53
52
: this . secure
54
53
? 443
55
54
: 80 ) ;
56
- this . query = opts . query || { } ;
57
- if ( "string" === typeof this . query ) this . query = parseqs . decode ( this . query ) ;
58
- this . upgrade = false !== opts . upgrade ;
59
- this . path = ( opts . path || "/engine.io" ) . replace ( / \/ $ / , "" ) + "/" ;
60
- this . forceJSONP = ! ! opts . forceJSONP ;
61
- this . jsonp = false !== opts . jsonp ;
62
- this . forceBase64 = ! ! opts . forceBase64 ;
63
- this . enablesXDR = ! ! opts . enablesXDR ;
64
- this . withCredentials = false !== opts . withCredentials ;
65
- this . timestampParam = opts . timestampParam || "t" ;
66
- this . timestampRequests = opts . timestampRequests ;
55
+
67
56
this . transports = opts . transports || [ "polling" , "websocket" ] ;
68
- this . transportOptions = opts . transportOptions || { } ;
69
57
this . readyState = "" ;
70
58
this . writeBuffer = [ ] ;
71
59
this . prevBufferLen = 0 ;
72
- this . policyPort = opts . policyPort || 843 ;
73
- this . rememberUpgrade = opts . rememberUpgrade || false ;
74
- this . binaryType = null ;
75
- this . onlyBinaryUpgrades = opts . onlyBinaryUpgrades ;
76
- this . perMessageDeflate =
77
- false !== opts . perMessageDeflate ? opts . perMessageDeflate || { } : false ;
78
-
79
- if ( true === this . perMessageDeflate ) this . perMessageDeflate = { } ;
80
- if ( this . perMessageDeflate && null == this . perMessageDeflate . threshold ) {
81
- this . perMessageDeflate . threshold = 1024 ;
82
- }
83
60
84
- // SSL options for Node.js client
85
- this . pfx = opts . pfx || null ;
86
- this . key = opts . key || null ;
87
- this . passphrase = opts . passphrase || null ;
88
- this . cert = opts . cert || null ;
89
- this . ca = opts . ca || null ;
90
- this . ciphers = opts . ciphers || null ;
91
- this . rejectUnauthorized =
92
- opts . rejectUnauthorized === undefined ? true : opts . rejectUnauthorized ;
93
- this . forceNode = ! ! opts . forceNode ;
61
+ this . opts = Object . assign (
62
+ {
63
+ path : "/engine.io" ,
64
+ agent : false ,
65
+ upgrade : true ,
66
+ jsonp : true ,
67
+ timestampParam : "t" ,
68
+ policyPort : 843 ,
69
+ rememberUpgrade : false ,
70
+ rejectUnauthorized : true ,
71
+ perMessageDeflate : {
72
+ threshold : 1024
73
+ } ,
74
+ transportOptions : { }
75
+ } ,
76
+ opts
77
+ ) ;
78
+
79
+ this . opts . path = this . opts . path . replace ( / \/ $ / , "" ) + "/" ;
80
+
81
+ if ( typeof this . opts . query === "string" ) {
82
+ this . opts . query = parseqs . decode ( this . opts . query ) ;
83
+ }
94
84
95
85
// detect ReactNative environment
96
- this . isReactNative =
86
+ this . opts . isReactNative =
97
87
typeof navigator !== "undefined" &&
98
88
typeof navigator . product === "string" &&
99
89
navigator . product . toLowerCase ( ) === "reactnative" ;
100
90
101
- // other options for Node.js or ReactNative client
102
- if ( typeof self === "undefined" || this . isReactNative ) {
103
- if ( opts . extraHeaders && Object . keys ( opts . extraHeaders ) . length > 0 ) {
104
- this . extraHeaders = opts . extraHeaders ;
105
- }
106
-
107
- if ( opts . localAddress ) {
108
- this . localAddress = opts . localAddress ;
109
- }
110
- }
111
-
112
91
// set on handshake
113
92
this . id = null ;
114
93
this . upgrades = null ;
@@ -130,53 +109,33 @@ class Socket extends Emitter {
130
109
*/
131
110
createTransport ( name ) {
132
111
debug ( 'creating transport "%s"' , name ) ;
133
- const query = clone ( this . query ) ;
112
+ const query = clone ( this . opts . query ) ;
134
113
135
114
// append engine.io protocol identifier
136
115
query . EIO = parser . protocol ;
137
116
138
117
// transport name
139
118
query . transport = name ;
140
119
141
- // per-transport options
142
- const options = this . transportOptions [ name ] || { } ;
143
-
144
120
// session id if we already have one
145
121
if ( this . id ) query . sid = this . id ;
146
122
147
- const transport = new transports [ name ] ( {
148
- query : query ,
149
- socket : this ,
150
- agent : options . agent || this . agent ,
151
- hostname : options . hostname || this . hostname ,
152
- port : options . port || this . port ,
153
- secure : options . secure || this . secure ,
154
- path : options . path || this . path ,
155
- forceJSONP : options . forceJSONP || this . forceJSONP ,
156
- jsonp : options . jsonp || this . jsonp ,
157
- forceBase64 : options . forceBase64 || this . forceBase64 ,
158
- enablesXDR : options . enablesXDR || this . enablesXDR ,
159
- withCredentials : options . withCredentials || this . withCredentials ,
160
- timestampRequests : options . timestampRequests || this . timestampRequests ,
161
- timestampParam : options . timestampParam || this . timestampParam ,
162
- policyPort : options . policyPort || this . policyPort ,
163
- pfx : options . pfx || this . pfx ,
164
- key : options . key || this . key ,
165
- passphrase : options . passphrase || this . passphrase ,
166
- cert : options . cert || this . cert ,
167
- ca : options . ca || this . ca ,
168
- ciphers : options . ciphers || this . ciphers ,
169
- rejectUnauthorized : options . rejectUnauthorized || this . rejectUnauthorized ,
170
- perMessageDeflate : options . perMessageDeflate || this . perMessageDeflate ,
171
- extraHeaders : options . extraHeaders || this . extraHeaders ,
172
- forceNode : options . forceNode || this . forceNode ,
173
- localAddress : options . localAddress || this . localAddress ,
174
- requestTimeout : options . requestTimeout || this . requestTimeout ,
175
- protocols : options . protocols || void 0 ,
176
- isReactNative : this . isReactNative
177
- } ) ;
178
-
179
- return transport ;
123
+ const opts = Object . assign (
124
+ {
125
+ query,
126
+ socket : this ,
127
+ hostname : this . hostname ,
128
+ secure : this . secure ,
129
+ port : this . port
130
+ } ,
131
+ this . opts . transportOptions [ name ] ,
132
+ this . opts
133
+ ) ;
134
+
135
+ // console.log(opts);
136
+ debug ( "options: %j" , opts ) ;
137
+
138
+ return new transports [ name ] ( opts ) ;
180
139
}
181
140
182
141
/**
@@ -187,7 +146,7 @@ class Socket extends Emitter {
187
146
open ( ) {
188
147
let transport ;
189
148
if (
190
- this . rememberUpgrade &&
149
+ this . opts . rememberUpgrade &&
191
150
Socket . priorWebsocketSuccess &&
192
151
this . transports . indexOf ( "websocket" ) !== - 1
193
152
) {
@@ -208,6 +167,7 @@ class Socket extends Emitter {
208
167
try {
209
168
transport = this . createTransport ( transport ) ;
210
169
} catch ( e ) {
170
+ debug ( "error while creating transport: %s" , e ) ;
211
171
this . transports . shift ( ) ;
212
172
this . open ( ) ;
213
173
return ;
@@ -381,7 +341,11 @@ class Socket extends Emitter {
381
341
382
342
// we check for `readyState` in case an `open`
383
343
// listener already closed the socket
384
- if ( "open" === this . readyState && this . upgrade && this . transport . pause ) {
344
+ if (
345
+ "open" === this . readyState &&
346
+ this . opts . upgrade &&
347
+ this . transport . pause
348
+ ) {
385
349
debug ( "starting upgrade probes" ) ;
386
350
let i = 0 ;
387
351
const l = this . upgrades . length ;
0 commit comments