fix signalR

This commit is contained in:
Peter Maquiran
2024-02-01 11:44:56 +01:00
parent e21e5da7e5
commit 2d6598edfe
3 changed files with 41 additions and 26 deletions
@@ -70,16 +70,13 @@ export class SocketConnectionMCRService {
class ReconnectingWebSocketSignalR {
private connection: any
isOpen: boolean
isOpen: boolean = false
private callbacks: Function[] = []
private onDisconnect: Function[] = []
private onConnect: Function[] = []
private stop = true
constructor() {
this.isOpen = false;
this.connect();
}
constructor() {}
connect() {
console.log("try to connect=================================")
@@ -113,11 +110,11 @@ class ReconnectingWebSocketSignalR {
})
this.connection.onclose((error) => {
console.log('WebSocket connection closed');
console.log('WebSocket connection closed..');
this.isOpen = false;
this.onDisconnect.forEach(callback => callback());
// Attempt to reconnect after a delay
if(this.stop) {
if(this.stop == false) {
setTimeout(() => {
this.connect();
}, 1000); // Adjust the delay as needed
@@ -128,16 +125,18 @@ class ReconnectingWebSocketSignalR {
disconnect() {
this.stop = true
this.connection.stop()
.then(() => {
console.log('WebSocket connection closed');
this.isOpen = false;
this.onDisconnect.forEach(callback => callback());
console.log("SignalR connection stopped.");
})
.catch((error) => {
console.error("Error stopping SignalR connection:", error);
});
if(this.isOpen == true) {
this.connection.stop()
.then(() => {
console.log('WebSocket connection was closed by client');
this.isOpen = false;
this.onDisconnect.forEach(callback => callback());
console.log("SignalR connection stopped.");
})
.catch((error) => {
console.error("Error stopping SignalR connection by client:", error);
});
}
}
subscribe(callback) {
@@ -160,7 +159,7 @@ interface socketResponse {
index: string
Guid: string
isCompleted: Boolean
IsCompleted: Boolean
}
// class ReconnectingWebSocket {
@@ -256,7 +255,7 @@ interface socketResponse {
// // })
// // this.socket.subscribe((data: socketResponse) => {
// // if(data.isCompleted == true) {
// // if(data.IsCompleted == true) {
// // this.callbacks[data.Guid](data)
// // delete this.callbacks[data.Guid]
// // }
@@ -302,38 +301,48 @@ export class ObjectMergeNotification{
callbacks: {[GUID: string]: Function} = {}
runWatch = true
CMAPIService: CMAPIService = window["CMAPIAPIRepository"]
watchCount = 0
constructor() {
this.socket.onDisconnectCallback(()=> {
console.log("run watch")
this.runWatch = true
this.watch()
})
this.socket.onConnectCallback(()=> {
console.log("open trigger")
this.runWatch = false
})
this.socket.subscribe((data: socketResponse) => {
if(data.isCompleted == true) {
if(data.IsCompleted == true) {
console.log("==================!!!====================")
this.callbacks[data.Guid](data)
delete this.callbacks[data.Guid]
} else {
console.log("else", data)
}
})
this.socket.connect();
this.watch()
}
close() {
console.log("close=============================================")
this.socket.disconnect();
this.watchCount = 0;
this.runWatch = false
}
async watch() {
this.watchCount = 0;
if(this.runWatch) {
setTimeout(async ()=> {
setTimeout(async () => {
for(const [key, funx] of Object.entries(this.callbacks)) {
const request = await this.CMAPIService.getVideoHeader(key)
@@ -344,7 +353,13 @@ export class ObjectMergeNotification{
}
}
this.watch()
this.watchCount++
if(this.watchCount <= 15) {
this.watch()
} else {
this.runWatch = false
}
}, 1000)