@@ -50,6 +50,30 @@ export class Echo extends BaseEcho {
50
50
return defu ( headers , this . options . auth . headers )
51
51
}
52
52
53
+ async connect ( ) {
54
+ if ( this . config && this . config . onBeforeConnect ) {
55
+ await this . config . onBeforeConnect ( )
56
+ }
57
+
58
+ super . connect ( )
59
+
60
+ if ( this . config && this . config . onAfterConnect ) {
61
+ await this . config . onAfterConnect ( )
62
+ }
63
+ }
64
+
65
+ async disconnect ( ) {
66
+ if ( this . config && this . config . onBeforeDisconnect ) {
67
+ await this . config . onBeforeDisconnect ( )
68
+ }
69
+
70
+ super . disconnect ( )
71
+
72
+ if ( this . config && this . config . onAfterDisconnect ) {
73
+ await this . config . onAfterDisconnect ( )
74
+ }
75
+ }
76
+
53
77
watchAuthState ( ) {
54
78
if ( this . config . authModule && this . ctx . app . $auth ) {
55
79
this . ctx . app . $auth . $storage . watchState ( 'loggedIn' , async ( loggedIn : boolean ) => {
@@ -58,28 +82,11 @@ export class Echo extends BaseEcho {
58
82
if ( this . config . connectOnLogin && loggedIn ) {
59
83
// set broadcaster when user logged in
60
84
this . options . broadcaster = this . config . broadcaster
61
-
62
- if ( this . config . onBeforeConnect ) {
63
- await this . config . onBeforeConnect ( )
64
- }
65
-
66
- this . connect ( )
67
-
68
- if ( this . config . onAfterConnect ) {
69
- await this . config . onAfterConnect ( )
70
- }
85
+ await this . connect ( )
71
86
}
72
87
73
88
if ( this . config . disconnectOnLogout && ! loggedIn && this . connector ) {
74
- if ( this . config . onBeforeDisconnect ) {
75
- await this . config . onBeforeDisconnect ( )
76
- }
77
-
78
- this . disconnect ( )
79
-
80
- if ( this . config . onAfterDisconnect ) {
81
- await this . config . onAfterDisconnect ( )
82
- }
89
+ await this . disconnect ( )
83
90
}
84
91
} ) . bind ( this )
85
92
}
0 commit comments