1- const WebSocketClient = require ( './WebSocketClient.js' )
1+ const fs = nativeRequire ( 'fs' ) ;
2+
3+ const WebSocketClient = require ( './WebSocketClient.js' ) ;
4+
5+ const SIB_SESSION_FILE = 'sibelius-session.json'
26
37class SibeliusConnect extends WebSocketClient {
48 constructor ( { appName = 'Sibelius Connect Remote' , callbackAddress = '/SibeliusCallback' , port = 1898 , plugins = [ ] } = { } ) {
@@ -10,53 +14,109 @@ class SibeliusConnect extends WebSocketClient {
1014 this . handshakeDone = false ;
1115 }
1216
13- onOpen ( ) {
14- this . sendHandshake ( ) ;
17+ onOpen ( resolve ) {
18+ // reset the message queue so we don't send a bunch of queued commands
19+ // on reconnect, and because we need to send the handshake message first
20+ this . messageQueue = [ ] ;
21+ this . _sendHandshake ( ) ;
22+
23+ super . onOpen ( resolve ) ;
1524 }
1625
17- sendHandshake ( ) {
26+ _sendHandshake ( ) {
1827 const message = {
1928 handshakeVersion : '1.0' ,
2029 clientName : this . appName ,
2130 message : 'connect' ,
2231 } ;
2332
33+ const sessionData = loadJSON ( SIB_SESSION_FILE , ( e ) => {
34+ console . log ( `${ SIB_SESSION_FILE } not found - starting new session` )
35+ } ) ;
36+ this . sessionToken = sessionData ?. sessionToken ;
37+
2438 if ( this . sessionToken ) {
2539 message . sessionToken = this . sessionToken ;
2640 } else if ( this . plugins . length > 0 ) {
2741 // We can't send a new list of plugins unless we're starting a new session
2842 message . plugins = this . plugins ;
2943 }
3044
31- this . sendMessage ( message ) ;
45+ this . send ( message ) ;
46+
47+ if ( message . sessionToken ) {
48+ // Sibelius doesn't send a response if reconnecting with a sessionToken,
49+ // so we will simulate receiving the sessionToken again
50+ this . onMessage ( { data : JSON . stringify ( sessionData ) } )
51+ }
3252 }
3353
34- onMessage ( data ) {
54+ _processHandshake ( data ) {
55+ console . log ( data )
3556 if ( data . sessionToken ) {
3657 this . sessionToken = data . sessionToken ;
3758 console . log ( 'Received sessionToken:' , this . sessionToken ) ;
38- this . handshakeDone = true ; // Handshake is now complete
39- this . sendQueuedMessages ( ) ; // Now safe to send queued messages
59+ saveJSON ( SIB_SESSION_FILE , data , ( e ) => console . log ( "unable to save sessionToken" , e ) ) ;
60+ this . handshakeDone = true ;
61+ }
62+ else {
63+ console . error ( "Handshake failed" ) ;
64+ }
65+ }
66+
67+ onMessage ( event ) {
68+ const data = JSON . parse ( event . data ) ;
69+
70+ if ( ! this . handshakeDone && data . sessionToken ) {
71+ this . _processHandshake ( data ) ;
4072 }
4173
74+ super . onMessage ( event ) ;
75+
4276 if ( this . callbackAddress && this . callbackAddress . length > 0 ) {
43- receive ( this . callbackAddress , data )
77+ receive ( this . callbackAddress , data ) ;
4478 }
4579 }
4680
4781 onClose ( event ) {
4882 if ( event . code === 1000 ) {
49- console . log ( 'Connection closed cleanly - send a command to open a new connection' ) ;
83+ console . log ( 'Connection closing cleanly - will attempt a fresh connection' ) ;
5084 this . sessionToken = null ;
5185 this . handshakeDone = false ;
52- } else {
53- console . log ( 'Connection lost, retrying...' ) ;
86+ this . cleanupSocket ( ) ;
87+ this . shouldReconnect = true ;
88+
89+ this . _removeSessionFile ( `${ __dirname } /${ SIB_SESSION_FILE } ` ) ;
90+ }
91+
92+ super . onClose ( event )
93+ }
94+
95+ _removeSessionFile ( file ) {
96+ if ( ! fs . existsSync ( file ) ) {
97+ return ;
5498 }
99+ fs . unlink ( file , ( error ) => {
100+ if ( error ) {
101+ console . warn ( `Error removing ${ file } ` , error ) ;
102+ this . shouldReconnect = false ;
103+ return ;
104+ }
105+
106+ console . log ( `Removed ${ file } ` ) ;
107+ } )
55108 }
56109
57- close ( ) {
58- if ( this . socket ?. readyState === WebSocket . OPEN ) {
59- this . socket . close ( 1000 , 'Unload' ) ;
110+ async send ( message ) {
111+ if ( ! this . socket ) {
112+ await this . connect ( )
113+ }
114+
115+ try {
116+ super . send ( message ) ;
117+ }
118+ catch ( e ) {
119+ console . error ( 'caught error' , e )
60120 }
61121 }
62122}
0 commit comments