File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ export class Session {
4242 private _baseSession : IBaseSession ;
4343 private _wampSession : WAMPSession ;
4444 private _idGen : SessionScopeIDGenerator = new SessionScopeIDGenerator ( ) ;
45+ private _onDisconnect ?: ( ) => void ;
4546
4647 private _callRequests : Map < number , {
4748 resolve : ( value : Result ) => void ,
@@ -75,6 +76,7 @@ export class Session {
7576 constructor ( baseSession : IBaseSession ) {
7677 this . _baseSession = baseSession ;
7778 this . _wampSession = new WAMPSession ( baseSession . serializer ( ) ) ;
79+ this . _baseSession . setOnDisconnect ( ( ) => this . markDisconnected ( ) ) ;
7880
7981 ( async ( ) => {
8082 for ( ; ; ) {
@@ -84,6 +86,10 @@ export class Session {
8486 } ) ( ) ;
8587 }
8688
89+ setDisconnectCallback ( callback : ( ) => void ) : void {
90+ this . _onDisconnect = callback ;
91+ }
92+
8793 private get _nextID ( ) : number {
8894 return this . _idGen . next ( ) ;
8995 }
@@ -226,6 +232,10 @@ export class Session {
226232 }
227233
228234 private markDisconnected ( ) {
235+ if ( this . _onDisconnect ) {
236+ this . _onDisconnect ( ) ;
237+ }
238+
229239 if ( ! this . isConnected ( ) ) {
230240 return
231241 }
Original file line number Diff line number Diff line change @@ -48,13 +48,18 @@ export abstract class IBaseSession {
4848 isConnected ( ) : boolean {
4949 throw new Error ( "UnimplementedError" ) ;
5050 }
51+
52+ setOnDisconnect ( callback : ( ) => void ) : void {
53+ throw new Error ( "UnimplementedError" ) ;
54+ }
5155}
5256
5357export class BaseSession extends IBaseSession {
5458 private readonly _ws : WebSocket ;
5559 private readonly _wsMessageHandler : any ;
5660 private readonly sessionDetails : SessionDetails ;
5761 private readonly _serializer : Serializer ;
62+ private _onDisconnect ?: ( ) => void ;
5863
5964 constructor (
6065 ws : WebSocket ,
@@ -70,6 +75,9 @@ export class BaseSession extends IBaseSession {
7075
7176 // close cleanly on abrupt client disconnect
7277 this . _ws . addEventListener ( "close" , async ( ) => {
78+ if ( this . _onDisconnect ) {
79+ this . _onDisconnect ( ) ;
80+ }
7381 await this . close ( ) ;
7482 } ) ;
7583 }
@@ -134,6 +142,10 @@ export class BaseSession extends IBaseSession {
134142 isConnected ( ) : boolean {
135143 return this . _ws . readyState === WebSocket . OPEN ;
136144 }
145+
146+ setOnDisconnect ( callback : ( ) => void ) : void {
147+ this . _onDisconnect = callback ;
148+ }
137149}
138150
139151export class Result {
You can’t perform that action at this time.
0 commit comments