Skip to content

Commit a30e28a

Browse files
committed
Small changes and (hopefully) properly catching errors properly for all situations
1 parent c83df0b commit a30e28a

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

DoricoRemote.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// const fs = nativeRequire('fs');
1+
const fs = nativeRequire('fs');
22

33
const WebSocketClient = require('./WebSocketClient.js')
44

@@ -14,7 +14,7 @@ class DoricoRemote extends WebSocketClient {
1414
}
1515

1616
onOpen(resolve) {
17-
this.on('handshakeDone', () => super.onOpen(resolve));
17+
this.once('handshakeDone', () => super.onOpen(resolve));
1818

1919
this._sendHandshake();
2020

@@ -82,39 +82,42 @@ class DoricoRemote extends WebSocketClient {
8282
onClose(event) {
8383
if (event.code === 1000) {
8484
console.log('Connection closing cleanly - will attempt a fresh connection');
85-
// this.sessionToken = null;
86-
// this.handshakeDone = false;
85+
86+
this._removeSessionFile(`${__dirname}/${DOR_SESSION_FILE}`);
87+
this.sessionToken = null;
88+
8789
this.cleanupSocket();
88-
// this.shouldReconnect = true;
8990

90-
// this._removeSessionFile(`${__dirname}/${DOR_SESSION_FILE}`);
91+
this.shouldReconnect = true;
9192
}
9293

94+
this.handshakeDone = false;
95+
9396
super.onClose(event)
9497
}
9598

96-
// _removeSessionFile(file) {
97-
// if (!fs.existsSync(file)) {
98-
// return;
99-
// }
100-
// fs.unlink(file, (error) => {
101-
// if (error) {
102-
// console.warn(`Error removing ${file}`, error);
103-
// this.shouldReconnect = false;
104-
// return;
105-
// }
106-
107-
// console.log(`Removed ${file}`);
108-
// })
109-
// }
110-
111-
send(message) {
99+
_removeSessionFile(file) {
100+
if (!fs.existsSync(file)) {
101+
return;
102+
}
103+
fs.unlink(file, (error) => {
104+
if (error) {
105+
console.warn(`Error removing ${file}`, error);
106+
this.shouldReconnect = false;
107+
return;
108+
}
109+
110+
console.log(`Removed ${file}`);
111+
})
112+
}
113+
114+
async send(message) {
112115
if (!this.socket) {
113116
this.connect();
114117
}
115118

116119
try {
117-
super.send(message);
120+
await super.send(message)//.catch(e => console.error(e.message));
118121
}
119122
catch (e) {
120123
console.error(e.message)

SibeliusConnect.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class SibeliusConnect extends WebSocketClient {
4848
if (message.sessionToken) {
4949
// Sibelius doesn't send a response if reconnecting with a sessionToken,
5050
// so we will simulate receiving the sessionToken again
51+
this.handshakeDone = false;
5152
this.onMessage({ data: JSON.stringify(sessionData) })
5253
}
5354
}
@@ -84,14 +85,17 @@ class SibeliusConnect extends WebSocketClient {
8485
onClose(event) {
8586
if (event.code === 1000) {
8687
console.log('Connection closing cleanly - will attempt a fresh connection');
88+
89+
this._removeSessionFile(`${__dirname}/${SIB_SESSION_FILE}`);
8790
this.sessionToken = null;
88-
this.handshakeDone = false;
91+
8992
this.cleanupSocket();
90-
this.shouldReconnect = true;
9193

92-
this._removeSessionFile(`${__dirname}/${SIB_SESSION_FILE}`);
94+
this.shouldReconnect = true;
9395
}
9496

97+
this.handshakeDone = false;
98+
9599
super.onClose(event)
96100
}
97101

@@ -110,13 +114,13 @@ class SibeliusConnect extends WebSocketClient {
110114
})
111115
}
112116

113-
send(message) {
117+
async send(message) {
114118
if (!this.socket) {
115119
this.connect();
116120
}
117121

118122
try {
119-
super.send(message);
123+
await super.send(message)//.catch(e => console.error(e.message));
120124
}
121125
catch (e) {
122126
console.error(e.message)

WebSocketClient.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class WebSocketClient extends EventEmitter {
4444

4545
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
4646
console.error("Max reconnect attempts reached");
47+
this.shouldReconnect = false;
4748
this.cleanupSocket();
4849
this.emit("reconnect_failed");
4950
return;
@@ -87,7 +88,7 @@ class WebSocketClient extends EventEmitter {
8788
this.reconnectAttempts = 0;
8889
this.shouldReconnect = false;
8990
this.processingQueue = false;
90-
91+
9192
// Reject pending promises in the message queue
9293
while (this.messageQueue.length) {
9394
const { reject } = this.messageQueue.shift();
@@ -104,7 +105,6 @@ class WebSocketClient extends EventEmitter {
104105
this.emit("open");
105106
resolve();
106107
this.processingQueue = false;
107-
console.log(this.messageQueue);
108108
this._processQueue();
109109
}
110110

0 commit comments

Comments
 (0)