Skip to content

Commit b7f9358

Browse files
committed
updated
1 parent e609dd3 commit b7f9358

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

src/index.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,36 @@ class Codebolt { // Extend EventEmitter
5858
* @description Waits for the WebSocket connection to open.
5959
* @returns {Promise<void>} A promise that resolves when the WebSocket connection is open.
6060
*/
61-
async waitForConnection() {
62-
return new Promise<void>((resolve, reject) => {
63-
if (!this.websocket) {
64-
reject(new Error('WebSocket is not initialized'));
65-
return;
66-
}
61+
// async waitForConnection() {
62+
// return new Promise<void>((resolve, reject) => {
63+
// if (!this.websocket) {
64+
// reject(new Error('WebSocket is not initialized'));
65+
// return;
66+
// }
6767

68-
if (this.websocket.readyState === WebSocket.OPEN) {
69-
resolve();
70-
return;
71-
}
68+
// if (this.websocket.readyState === WebSocket.OPEN) {
69+
// resolve();
70+
// return;
71+
// }
7272

73-
this.websocket.addEventListener('open', () => {
74-
resolve();
75-
});
73+
// this.websocket.addEventListener('open', () => {
74+
// resolve();
75+
// });
7676

77-
this.websocket.addEventListener('error', (error) => {
78-
reject(error);
79-
});
77+
// this.websocket.addEventListener('error', (error) => {
78+
// reject(error);
79+
// });
8080

81-
});
82-
}
81+
// });
82+
// }
8383

8484
async connect() {
8585
await this.wsManager.connect();
8686
}
8787

8888
async disconnect() {
8989
await this.wsManager.disconnect();
90+
this.wsManager = null;
9091
}
9192

9293
websocket: WebSocket | null = null;

src/modules/websocket.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,27 @@ import yaml from 'js-yaml';
66
* Class representing a WebSocket connection.
77
*/
88
class cbws {
9-
websocket: WebSocket;
9+
private websocket: WebSocket | null = null;
1010

1111
/**
1212
* Constructs a new cbws instance and initializes the WebSocket connection.
1313
*/
14-
constructor() {
14+
async connect(): Promise<WebSocket> {
15+
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
16+
return this.websocket;
17+
}
18+
1519
const uniqueConnectionId = this.getUniqueConnectionId();
1620
const initialMessage = this.getInitialMessage();
17-
console.log(uniqueConnectionId)
1821
this.websocket = new WebSocket(`ws://localhost:${process.env.SOCKET_PORT}/codebolt?id=${uniqueConnectionId}`);
19-
this.initializeWebSocket(initialMessage).catch(error => {
20-
console.error("WebSocket connection failed:", error);
21-
});
22+
return this.initializeWebSocket(initialMessage);
23+
}
24+
25+
async disconnect(): Promise<void> {
26+
if (this.websocket) {
27+
this.websocket.close();
28+
this.websocket = null;
29+
}
2230
}
2331
private getUniqueConnectionId(): string {
2432
try {
@@ -49,20 +57,18 @@ class cbws {
4957
*/
5058
private async initializeWebSocket(initialMessage: string): Promise<WebSocket> {
5159
return new Promise((resolve, reject) => {
60+
if (!this.websocket) {
61+
reject(new Error('WebSocket is not initialized'));
62+
return;
63+
}
5264
this.websocket.on('error', (error: Error) => {
5365
console.log('WebSocket error:', error);
5466
reject(error);
5567
});
5668

5769
this.websocket.on('open', () => {
5870
console.log('WebSocket connected');
59-
// if (this.websocket) {
60-
// this.websocket.send(JSON.stringify({
61-
// "type": "sendMessage",
62-
// "message": initialMessage
63-
// }));
64-
// resolve(this.websocket);
65-
// }
71+
resolve(this.websocket!);
6672
});
6773

6874
this.websocket.on('message', (data: WebSocket.Data) => {
@@ -77,13 +83,12 @@ class cbws {
7783
* @returns {WebSocket} The WebSocket instance.
7884
* @throws {Error} If the WebSocket is not open.
7985
*/
80-
get getWebsocket(): WebSocket {
81-
if (!this.websocket.OPEN) {
86+
getWebsocket(): WebSocket {
87+
if (!this.websocket || this.websocket.readyState !== WebSocket.OPEN) {
8288
throw new Error('WebSocket is not open');
83-
} else {
84-
return this.websocket;
8589
}
90+
return this.websocket;
8691
}
8792
}
8893

89-
export default new cbws();
94+
export default cbws;

0 commit comments

Comments
 (0)