video merge

This commit is contained in:
Peter Maquiran
2024-02-07 16:45:53 +01:00
parent 4c56fa819c
commit 60edd1ca01
11 changed files with 586 additions and 421 deletions
+247 -215
View File
@@ -14,9 +14,9 @@ export class SocketConnectionMCRService {
// private onDisconnect: Function[] = []
// private onConnect: Function[] = []
// constructor(private http: HttpClient,) {
// window["http"] = this.http
// }
constructor(private http: HttpClient, private _CMAPIService: CMAPIService) {
window["http"] = this.http
}
// connect() {
@@ -67,137 +67,52 @@ export class SocketConnectionMCRService {
}
// class ReconnectingWebSocketSignalR {
class ReconnectingWebSocketSignalR {
// private connection: any
// isOpen: boolean = false
// private callbacks: Function[] = []
// private onDisconnect: Function[] = []
// private onConnect: Function[] = []
// private stop = true
// constructor() {}
// connect() {
// console.log("try to connect=================================")
// this.stop = false
// this.connection = new signalR.HubConnectionBuilder()
// .withUrl("https://gdcmapi-dev.dyndns.info/FileHub", {
// transport: signalR.HttpTransportType.LongPolling,
// accessTokenFactory: () => SessionStore.user.Authorization
// }).configureLogging(signalR.LogLevel.Information)
// .build();
// this.connection.start()
// .then(() => {
// this.isOpen = true;
// console.log('WebSocket connection established');
// this.onConnect.forEach(callback => callback());
// })
// .catch((error) => {
// console.error("Error starting SignalR connection:", error);
// });
// this.connection.on("ReceiveMessage", (message) => {
// const data: any = JSON.parse(message)
// console.log("ReceiveMessage", data)
// this.callbacks.forEach(callback => callback(data));
// })
// this.connection.onclose((error) => {
// console.log('WebSocket connection closed..');
// this.isOpen = false;
// this.onDisconnect.forEach(callback => callback());
// // Attempt to reconnect after a delay
// if(this.stop == false) {
// setTimeout(() => {
// this.connect();
// }, 1000); // Adjust the delay as needed
// }
// });
// }
// disconnect() {
// this.stop = true
// 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) {
// this.callbacks.push(callback);
// }
// unsubscribe(callback) {
// this.callbacks = this.callbacks.filter(cb => cb !== callback);
// }
// onDisconnectCallback(callback) {
// this.onDisconnect.push(callback)
// }
// onConnectCallback(callback) {
// this.onConnect.push(callback)
// }
// }
interface socketResponse {
index: string
Guid: string
IsCompleted: Boolean
}
class ReconnectingWebSocket {
private url: string
private socket
isOpen: boolean
private connection: any
isOpen: boolean = false
private callbacks: Function[] = []
private onDisconnect: Function[] = []
private onConnect: Function[] = []
private whenConnected: Function[] = []
private stop = true
http: HttpClient = window["http"]
constructor(url) {
this.url = url;
this.socket = null;
this.isOpen = false;
this.connect();
}
constructor() {}
connect() {
this.socket = new WebSocket(this.url);
console.log("try to connect=================================")
this.stop = false
this.socket.addEventListener('open', (event) => {
this.connection = new signalR.HubConnectionBuilder()
.withUrl("https://gdcmapi-dev.dyndns.info/FileHub", {
transport: signalR.HttpTransportType.LongPolling,
accessTokenFactory: () => SessionStore.user.Authorization
}).configureLogging(signalR.LogLevel.Information)
.build();
this.connection.start()
.then(() => {
this.isOpen = true;
console.log('WebSocket connection established');
// Example: Send a message to the server
this.socket.send('Hello, WebSocket Server!');
this.onConnect.forEach(callback => callback());
this.whenConnected.forEach(callback => callback())
})
.catch((error) => {
console.error("Error starting SignalR connection:", error);
});
this.socket.addEventListener('message', (event) => {
const data: socketResponse = JSON.parse(event.data)
this.connection.on("ReceiveMessage", (message) => {
const data: any = JSON.parse(message)
console.log("ReceiveMessage", data)
this.callbacks.forEach(callback => callback(data));
});
})
this.socket.addEventListener('close', (event) => {
console.log('WebSocket connection closed');
this.connection.onclose((error) => {
console.log('WebSocket connection closed..');
this.isOpen = false;
this.onDisconnect.forEach(callback => callback());
// Attempt to reconnect after a delay
@@ -206,22 +121,27 @@ class ReconnectingWebSocket {
this.connect();
}, 1000); // Adjust the delay as needed
}
});
}
send(message) {
if (this.isOpen) {
this.socket.send(message);
} else {
console.error('WebSocket connection is not open. Unable to send message.');
}
commit(path) {
this.connection.invoke("CommitUpload", path).catch(err => console.error(err.toString()));
}
disconnect() {
this.stop = true
if (this.isOpen) {
this.isOpen = false;
this.socket.close();
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);
});
}
}
@@ -239,94 +159,116 @@ class ReconnectingWebSocket {
onConnectCallback(callback) {
this.onConnect.push(callback)
}
}
export class ObjectMergeNotification{
socket = new ReconnectingWebSocket('ws://localhost:3002');
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) {
console.log("==================!!!====================")
try {
this.callbacks[data.Guid](data)
delete this.callbacks[data.Guid]
} catch (error) {}
} else {
console.log("else", data)
}
})
this.watch()
}
async watch() {
this.watchCount = 0;
if(this.runWatch) {
setTimeout(async () => {
for(const [key, funx] of Object.entries(this.callbacks)) {
const request = await this.CMAPIService.getVideoHeader(key)
if(request.isOk()) {
funx()
delete this.callbacks[key]
}
}
this.watchCount++
if(this.watchCount <= 15) {
this.watch()
} else {
this.runWatch = false
}
}, 1000)
registerWhenConnected(f: Function) {
if(this.isOpen) {
f();
} else {
console.log("end loop============================")
this.whenConnected.push(f);
}
}
close() {
this.socket.disconnect();
this.watchCount = 0;
this.runWatch = false
}
subscribe(GUID, callback:Function) {
this.callbacks[GUID] = callback;
}
unsubscribe(GUID) {
delete this.callbacks[GUID]
}
}
interface socketResponse {
index: string
Guid: string
IsCompleted: Boolean
}
// class ReconnectingWebSocket {
// private url: string
// private socket
// isOpen: boolean
// private callbacks: Function[] = []
// private onDisconnect: Function[] = []
// private onConnect: Function[] = []
// private whenConnected: Function[] = []
// private stop = true
// http: HttpClient = window["http"]
// constructor(url) {
// this.url = url;
// this.socket = null;
// this.isOpen = false;
// }
// connect() {
// this.socket = new WebSocket(this.url);
// this.socket.addEventListener('open', (event) => {
// this.isOpen = true;
// console.log('WebSocket connection established');
// // Example: Send a message to the server
// this.socket.send('Hello, WebSocket Server!');
// this.onConnect.forEach(callback => callback());
// this.whenConnected.forEach(callback => callback())
// });
// this.socket.addEventListener('message', (event) => {
// const data: socketResponse = JSON.parse(event.data)
// this.callbacks.forEach(callback => callback(data));
// });
// this.socket.addEventListener('close', (event) => {
// console.log('WebSocket connection closed');
// this.isOpen = false;
// this.onDisconnect.forEach(callback => callback());
// // Attempt to reconnect after a delay
// if(this.stop == false) {
// setTimeout(() => {
// this.connect();
// }, 1000); // Adjust the delay as needed
// }
// });
// }
// send(message) {
// if (this.isOpen) {
// this.socket.send(message);
// } else {
// console.error('WebSocket connection is not open. Unable to send message.');
// }
// }
// disconnect() {
// this.stop = true
// if (this.isOpen) {
// this.isOpen = false;
// this.socket.close();
// }
// }
// subscribe(callback) {
// this.callbacks.push(callback);
// }
// unsubscribe(callback) {
// this.callbacks = this.callbacks.filter(cb => cb !== callback);
// }
// onDisconnectCallback(callback) {
// this.onDisconnect.push(callback)
// }
// onConnectCallback(callback) {
// this.onConnect.push(callback)
// }
// registerWhenConnected(f: Function) {
// if(this.isOpen) {
// f();
// } else {
// this.whenConnected.push(f);
// }
// }
// }
// export class ObjectMergeNotification{
// socket = new ReconnectingWebSocketSignalR()
// socket = new ReconnectingWebSocket('ws://localhost:3002');
// callbacks: {[GUID: string]: Function} = {}
// runWatch = true
// CMAPIService: CMAPIService = window["CMAPIAPIRepository"]
@@ -340,15 +282,14 @@ export class ObjectMergeNotification{
// })
// this.socket.onConnectCallback(()=> {
// console.log("open trigger")
// this.runWatch = false
// })
// this.socket.subscribe((data: socketResponse) => {
// if(data.IsCompleted == true) {
// console.log("==================!!!====================")
// try {
// try {
// this.callbacks[data.Guid](data)
// delete this.callbacks[data.Guid]
// } catch (error) {}
@@ -357,15 +298,11 @@ export class ObjectMergeNotification{
// }
// })
// this.socket.connect();
// this.watch()
// }
// close() {
// this.socket.disconnect();
// this.watchCount = 0;
// this.runWatch = false
// connect() {
// this.socket.connect()
// }
// async watch() {
@@ -386,11 +323,11 @@ export class ObjectMergeNotification{
// this.watchCount++
// if(this.watchCount <= 15) {
// this.watch()
// this.watch()
// } else {
// this.runWatch = false
// }
// }, 1000)
@@ -399,6 +336,12 @@ export class ObjectMergeNotification{
// }
// }
// close() {
// this.socket.disconnect();
// this.watchCount = 0;
// this.runWatch = false
// }
// subscribe(GUID, callback:Function) {
// this.callbacks[GUID] = callback;
// }
@@ -408,3 +351,92 @@ export class ObjectMergeNotification{
// }
// }
export class ObjectMergeNotification{
socket = new ReconnectingWebSocketSignalR()
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) {
console.log("==================!!!====================")
try {
this.callbacks[data.Guid](data)
delete this.callbacks[data.Guid]
} catch (error) {}
} else {
console.log("else", data)
}
})
this.socket.connect();
this.watch()
}
connect() {
this.socket.connect();
}
close() {
this.socket.disconnect();
this.watchCount = 0;
this.runWatch = false
}
async watch() {
// this.watchCount = 0;
// if(this.runWatch) {
// setTimeout(async () => {
// for(const [key, funx] of Object.entries(this.callbacks)) {
// const request = await this.CMAPIService.getVideoHeader(key)
// if(request.isOk()) {
// funx()
// delete this.callbacks[key]
// }
// }
// this.watchCount++
// if(this.watchCount <= 15) {
// this.watch()
// } else {
// this.runWatch = false
// }
// }, 1000)
// } else {
// console.log("end loop============================")
// }
}
subscribe(GUID, callback:Function) {
this.callbacks[GUID] = callback;
}
unsubscribe(GUID) {
delete this.callbacks[GUID]
}
}