@@ -52,6 +52,8 @@ var qz = (function() {
52
52
websocket : {
53
53
/** The actual websocket object managing the connection. */
54
54
connection : null ,
55
+ /** Track if a connection attempt is being cancelled. */
56
+ shutdown : false ,
55
57
56
58
/** Default parameters used on new connections. Override values using options parameter on {@link qz.websocket.connect}. */
57
59
connectConfig : {
@@ -75,6 +77,11 @@ var qz = (function() {
75
77
setup : {
76
78
/** Loop through possible ports to open connection, sets web socket calls that will settle the promise. */
77
79
findConnection : function ( config , resolve , reject ) {
80
+ if ( _qz . websocket . shutdown ) {
81
+ reject ( new Error ( "Connection attempt cancelled by user" ) ) ;
82
+ return ;
83
+ }
84
+
78
85
//force flag if missing ports
79
86
if ( ! config . port . secure . length ) {
80
87
if ( ! config . port . insecure . length ) {
@@ -90,6 +97,12 @@ var qz = (function() {
90
97
}
91
98
92
99
var deeper = function ( ) {
100
+ if ( _qz . websocket . shutdown ) {
101
+ //connection attempt was cancelled, bail out
102
+ reject ( new Error ( "Connection attempt cancelled by user" ) ) ;
103
+ return ;
104
+ }
105
+
93
106
config . port . portIndex ++ ;
94
107
95
108
if ( ( config . usingSecure && config . port . portIndex >= config . port . secure . length )
@@ -765,7 +778,9 @@ var qz = (function() {
765
778
} ,
766
779
767
780
isActive : function ( ) {
768
- return _qz . websocket . connection != null && _qz . websocket . connection . established ;
781
+ return ! _qz . websocket . shutdown && _qz . websocket . connection != null
782
+ && ( _qz . websocket . connection . readyState === _qz . tools . ws . OPEN
783
+ || _qz . websocket . connection . readyState === _qz . tools . ws . CONNECTING ) ;
769
784
} ,
770
785
771
786
assertActive : function ( ) {
@@ -1164,12 +1179,19 @@ var qz = (function() {
1164
1179
*/
1165
1180
connect : function ( options ) {
1166
1181
return _qz . tools . promise ( function ( resolve , reject ) {
1167
- if ( _qz . tools . isActive ( ) ) {
1168
- reject ( new Error ( "An open connection with QZ Tray already exists" ) ) ;
1169
- return ;
1170
- } else if ( _qz . websocket . connection != null ) {
1171
- reject ( new Error ( "The current connection attempt has not returned yet" ) ) ;
1172
- return ;
1182
+ if ( _qz . websocket . connection ) {
1183
+ const state = _qz . websocket . connection . readyState ;
1184
+
1185
+ if ( state === _qz . tools . ws . OPEN ) {
1186
+ reject ( new Error ( "An open connection with QZ Tray already exists" ) ) ;
1187
+ return ;
1188
+ } else if ( state === _qz . tools . ws . CONNECTING ) {
1189
+ reject ( new Error ( "The current connection attempt has not returned yet" ) ) ;
1190
+ return ;
1191
+ } else if ( state === _qz . tools . ws . CLOSING ) {
1192
+ reject ( new Error ( "Waiting for previous disconnect request to complete" ) ) ;
1193
+ return ;
1194
+ }
1173
1195
}
1174
1196
1175
1197
if ( ! _qz . tools . ws ) {
@@ -1197,6 +1219,7 @@ var qz = (function() {
1197
1219
options . host = [ options . host ] ;
1198
1220
}
1199
1221
1222
+ _qz . websocket . shutdown = false ; //reset state for new connection attempt
1200
1223
var attempt = function ( count ) {
1201
1224
var tried = false ;
1202
1225
var nextAttempt = function ( ) {
@@ -1236,11 +1259,17 @@ var qz = (function() {
1236
1259
*/
1237
1260
disconnect : function ( ) {
1238
1261
return _qz . tools . promise ( function ( resolve , reject ) {
1239
- if ( _qz . tools . isActive ( ) ) {
1240
- _qz . websocket . connection . close ( ) ;
1241
- _qz . websocket . connection . promise = { resolve : resolve , reject : reject } ;
1262
+ if ( _qz . websocket . connection != null ) {
1263
+ if ( _qz . tools . isActive ( ) ) {
1264
+ // handles closing both 'connecting' and 'connected' states
1265
+ _qz . websocket . shutdown = true ;
1266
+ _qz . websocket . connection . promise = { resolve : resolve , reject : reject } ;
1267
+ _qz . websocket . connection . close ( ) ;
1268
+ } else {
1269
+ reject ( new Error ( "Current connection is still closing" ) ) ;
1270
+ }
1242
1271
} else {
1243
- reject ( new Error ( "No open connection with QZ Tray" ) )
1272
+ reject ( new Error ( "No open connection with QZ Tray" ) ) ;
1244
1273
}
1245
1274
} ) ;
1246
1275
} ,
0 commit comments