@@ -12,6 +12,7 @@ var clone = require('clone');
1212var scErrors = require ( 'sc-errors' ) ;
1313var InvalidArgumentsError = scErrors . InvalidArgumentsError ;
1414var InvalidMessageError = scErrors . InvalidMessageError ;
15+ var InvalidActionError = scErrors . InvalidActionError ;
1516var SocketProtocolError = scErrors . SocketProtocolError ;
1617var TimeoutError = scErrors . TimeoutError ;
1718var BadConnectionError = scErrors . BadConnectionError ;
@@ -43,6 +44,7 @@ var SCSocket = function (opts) {
4344 // pingTimeout will be ackTimeout at the start, but it will
4445 // be updated with values provided by the 'connect' event
4546 this . pingTimeout = this . ackTimeout ;
47+ this . active = true ;
4648
4749 this . _clientMap = opts . clientMap || { } ;
4850
@@ -144,10 +146,13 @@ var SCSocket = function (opts) {
144146 self . disconnect ( ) ;
145147 } ;
146148
149+ if ( isBrowser && this . disconnectOnUnload && global . addEventListener ) {
150+ global . addEventListener ( 'beforeunload' , this . _unloadHandler , false ) ;
151+ }
152+ this . _clientMap [ this . clientId ] = this ;
153+
147154 if ( this . options . autoConnect ) {
148155 this . connect ( ) ;
149- } else {
150- this . activate ( ) ;
151156 }
152157} ;
153158
@@ -254,7 +259,11 @@ SCSocket.prototype.deauthenticate = function (callback) {
254259SCSocket . prototype . connect = SCSocket . prototype . open = function ( ) {
255260 var self = this ;
256261
257- this . activate ( ) ;
262+ if ( ! this . active ) {
263+ var error = new InvalidActionError ( 'Cannot connect a destroyed socket' ) ;
264+ this . _onSCError ( error ) ;
265+ return ;
266+ }
258267
259268 if ( this . state == this . CLOSED ) {
260269 this . pendingReconnect = false ;
@@ -316,14 +325,6 @@ SCSocket.prototype.disconnect = function (code, data) {
316325 }
317326} ;
318327
319- SCSocket . prototype . activate = function ( ) {
320- if ( isBrowser && this . disconnectOnUnload && global . addEventListener && ! this . active ) {
321- global . addEventListener ( 'beforeunload' , this . _unloadHandler , false ) ;
322- }
323- this . _clientMap [ this . clientId ] = this ;
324- this . active = true ;
325- } ;
326-
327328SCSocket . prototype . destroy = function ( code , data ) {
328329 if ( isBrowser && global . removeEventListener ) {
329330 global . removeEventListener ( 'beforeunload' , this . _unloadHandler , false ) ;
@@ -433,7 +434,6 @@ SCSocket.prototype.authenticate = function (signedAuthToken, callback) {
433434 var self = this ;
434435
435436 this . emit ( '#authenticate' , signedAuthToken , function ( err , authStatus ) {
436-
437437 if ( authStatus && authStatus . isAuthenticated != null ) {
438438 // If authStatus is correctly formatted (has an isAuthenticated property),
439439 // then we will rehydrate the authError.
@@ -915,7 +915,6 @@ SCSocket.prototype._tryUnsubscribe = function (channel) {
915915} ;
916916
917917SCSocket . prototype . unsubscribe = function ( channelName ) {
918-
919918 var channel = this . channels [ channelName ] ;
920919
921920 if ( channel ) {
0 commit comments