-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSibeliusConnect.js
131 lines (105 loc) · 3.78 KB
/
SibeliusConnect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const fs = nativeRequire('fs');
const WebSocketClient = require('./WebSocketClient.js');
const SIB_SESSION_FILE = 'sibelius-session.json'
class SibeliusConnect extends WebSocketClient {
constructor({ appName = 'Sibelius Connect Remote', callbackAddress = '/SibeliusCallback', port = 1898, plugins = [] } = {}) {
super(`ws://localhost:${port}`);
this.appName = appName;
this.callbackAddress = callbackAddress;
this.plugins = Array.isArray(plugins) ? plugins : [plugins];
this.sessionToken = null;
this.handshakeDone = false;
}
onOpen(resolve) {
this.on('handshakeDone', () => super.onOpen(resolve));
this._sendHandshake();
// super.onOpen(resolve);
}
_sendHandshake() {
const message = {
handshakeVersion: '1.0',
clientName: this.appName,
message: 'connect',
};
const sessionData = loadJSON(SIB_SESSION_FILE, (e) => {
console.log(`${SIB_SESSION_FILE} not found - starting new session`)
});
this.sessionToken = sessionData?.sessionToken;
if (this.sessionToken) {
message.sessionToken = this.sessionToken;
} else if (this.plugins.length > 0) {
// We can't send a new list of plugins unless we're starting a new session
message.plugins = this.plugins;
}
// use the socket's send method to avoid the messageQueue
this.socket.send(JSON.stringify(message));
console.log("Message sent:", message);
if (message.sessionToken) {
// Sibelius doesn't send a response if reconnecting with a sessionToken,
// so we will simulate receiving the sessionToken again
this.handshakeDone = false;
this.onMessage({ data: JSON.stringify(sessionData) })
}
}
_processHandshake(data) {
if (data.sessionToken) {
this.sessionToken = data.sessionToken;
console.log('Received sessionToken:', this.sessionToken);
saveJSON(SIB_SESSION_FILE, data, (e) => console.log("unable to save sessionToken", e));
this.handshakeDone = true;
this.emit('handshakeDone');
}
else {
console.error("Handshake failed");
}
}
onMessage(event) {
const data = JSON.parse(event.data);
if (!this.handshakeDone && data.sessionToken) {
this._processHandshake(data);
console.log("Message received:", data);
}
else {
super.onMessage(event);
}
if (this.callbackAddress && this.callbackAddress.length > 0) {
receive(this.callbackAddress, data);
}
}
onClose(event) {
if (event.code === 1000) {
console.log('Connection closing cleanly - will attempt a fresh connection');
this._removeSessionFile(`${__dirname}/${SIB_SESSION_FILE}`);
this.sessionToken = null;
this.cleanupSocket();
this.shouldReconnect = true;
}
this.handshakeDone = false;
super.onClose(event)
}
_removeSessionFile(file) {
if (!fs.existsSync(file)) {
return;
}
fs.unlink(file, (error) => {
if (error) {
console.warn(`Error removing ${file}`, error);
this.shouldReconnect = false;
return;
}
console.log(`Removed ${file}`);
})
}
async send(message) {
if (!this.socket) {
this.connect();
}
try {
await super.send(message)//.catch(e => console.error(e.message));
}
catch (e) {
console.error(e.message)
}
}
}
module.exports = SibeliusConnect