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'
2
6
3
7
class SibeliusConnect extends WebSocketClient {
4
8
constructor ( { appName = 'Sibelius Connect Remote' , callbackAddress = '/SibeliusCallback' , port = 1898 , plugins = [ ] } = { } ) {
@@ -10,53 +14,109 @@ class SibeliusConnect extends WebSocketClient {
10
14
this . handshakeDone = false ;
11
15
}
12
16
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 ) ;
15
24
}
16
25
17
- sendHandshake ( ) {
26
+ _sendHandshake ( ) {
18
27
const message = {
19
28
handshakeVersion : '1.0' ,
20
29
clientName : this . appName ,
21
30
message : 'connect' ,
22
31
} ;
23
32
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
+
24
38
if ( this . sessionToken ) {
25
39
message . sessionToken = this . sessionToken ;
26
40
} else if ( this . plugins . length > 0 ) {
27
41
// We can't send a new list of plugins unless we're starting a new session
28
42
message . plugins = this . plugins ;
29
43
}
30
44
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
+ }
32
52
}
33
53
34
- onMessage ( data ) {
54
+ _processHandshake ( data ) {
55
+ console . log ( data )
35
56
if ( data . sessionToken ) {
36
57
this . sessionToken = data . sessionToken ;
37
58
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 ) ;
40
72
}
41
73
74
+ super . onMessage ( event ) ;
75
+
42
76
if ( this . callbackAddress && this . callbackAddress . length > 0 ) {
43
- receive ( this . callbackAddress , data )
77
+ receive ( this . callbackAddress , data ) ;
44
78
}
45
79
}
46
80
47
81
onClose ( event ) {
48
82
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' ) ;
50
84
this . sessionToken = null ;
51
85
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 ;
54
98
}
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
+ } )
55
108
}
56
109
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 )
60
120
}
61
121
}
62
122
}
0 commit comments