File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ export class Session {
4141 private _baseSession : IBaseSession ;
4242 private _wampSession : WAMPSession ;
4343 private _idGen : SessionScopeIDGenerator = new SessionScopeIDGenerator ( ) ;
44+ private _onDisconnect ?: ( ) => void ;
4445
4546 private _callRequests : Map < number , {
4647 resolve : ( value : Result ) => void ,
@@ -74,6 +75,7 @@ export class Session {
7475 constructor ( baseSession : IBaseSession ) {
7576 this . _baseSession = baseSession ;
7677 this . _wampSession = new WAMPSession ( baseSession . serializer ( ) ) ;
78+ this . _baseSession . setOnDisconnect ( ( ) => this . markDisconnected ( ) ) ;
7779
7880 ( async ( ) => {
7981 for ( ; ; ) {
@@ -83,6 +85,10 @@ export class Session {
8385 } ) ( ) ;
8486 }
8587
88+ setDisconnectCallback ( callback : ( ) => void ) : void {
89+ this . _onDisconnect = callback ;
90+ }
91+
8692 private get _nextID ( ) : number {
8793 return this . _idGen . next ( ) ;
8894 }
@@ -225,6 +231,10 @@ export class Session {
225231 }
226232
227233 private markDisconnected ( ) {
234+ if ( this . _onDisconnect ) {
235+ this . _onDisconnect ( ) ;
236+ }
237+
228238 if ( ! this . isConnected ( ) ) {
229239 return
230240 }
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