video uploader

This commit is contained in:
Peter Maquiran
2024-01-29 13:49:53 +01:00
parent 892bdbe5cf
commit 2605d73baa
11 changed files with 757 additions and 179 deletions
@@ -1,24 +1,31 @@
import { Injectable } from '@angular/core';
import * as signalR from "@microsoft/signalr"
import { SessionStore } from '../store/session.service';
import { v4 as uuidv4 } from 'uuid'
import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http';
import { CMAPIService } from '../shared/repository/CMAPI/cmapi.service';
import { HubConnectionBuilder } from '@microsoft/signalr';
@Injectable({
providedIn: 'root'
})
export class SocketConnectionMCRService {
private callbacks: Function[] = []
private onDisconnect: Function[] = []
private onConnect: Function[] = []
constructor() { }
constructor(private http: HttpClient,) {
window["http"] = this.http
}
connect() {
var connection = new signalR.HubConnectionBuilder()
.withUrl("https://gdcmapi-dev.dyndns.info/FileHub", {
accessTokenFactory: () => SessionStore.user.Authorization
accessTokenFactory: () => "Bearer "+SessionStore.user.Authorization
}).configureLogging(signalR.LogLevel.Information)
.build();
connection.on("ReceiveMessage", (message) => {
console.log("ReceiveMessage", message)
})
@@ -43,4 +50,319 @@ export class SocketConnectionMCRService {
}
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)
}
}
class ReconnectingWebSocketSignalR {
private connection: any
isOpen: boolean
private callbacks: Function[] = []
private onDisconnect: Function[] = []
private onConnect: Function[] = []
constructor() {
this.isOpen = false;
this.connect();
}
connect() {
this.connection = new signalR.HubConnectionBuilder()
.withUrl("https://gdcmapi-dev.dyndns.info/FileHub", {
accessTokenFactory: () => "Bearer "+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());
console.log("SignalR connection started.");
})
.catch((error) => {
console.error("Error starting SignalR connection:", error);
});
this.connection.on("ReceiveMessage", (message) => {
const data: any = JSON.parse(message)
console.log(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
setTimeout(() => {
this.connect();
}, 1000); // Adjust the delay as needed
});
}
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 callbacks: Function[] = []
private onDisconnect: Function[] = []
private onConnect: Function[] = []
http: HttpClient = window["http"]
constructor(url) {
this.url = url;
this.socket = null;
this.isOpen = false;
this.connect();
}
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.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
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.');
}
}
close() {
if (this.isOpen) {
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)
}
}
// export class ObjectMergeNotification{
// //socket = new ReconnectingWebSocket('ws://localhost:3002');
// callbacks: {[GUID: string]: Function} = {}
// runWatch = true
// CMAPIService: CMAPIService = window["CMAPIAPIRepository"]
// constructor() {
// // this.socket.onDisconnectCallback(()=> {
// // this.runWatch = true
// // this.watch()
// // })
// // this.socket.onConnectCallback(()=> {
// // this.runWatch = false
// // })
// // this.socket.subscribe((data: socketResponse) => {
// // if(data.isCompleted == true) {
// // this.callbacks[data.Guid](data)
// // delete this.callbacks[data.Guid]
// // }
// // })
// this.watch()
// }
// async watch() {
// 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.watch()
// }, 1000)
// }
// }
// subscribe(GUID, callback:Function) {
// this.callbacks[GUID] = callback;
// }
// unsubscribe(GUID) {
// delete this.callbacks[GUID]
// }
// }
export class ObjectMergeNotification{
socket = new ReconnectingWebSocketSignalR()
callbacks: {[GUID: string]: Function} = {}
runWatch = true
CMAPIService: CMAPIService = window["CMAPIAPIRepository"]
constructor() {
this.socket.onDisconnectCallback(()=> {
this.runWatch = true
this.watch()
})
this.socket.onConnectCallback(()=> {
this.runWatch = false
})
this.socket.subscribe((data: socketResponse) => {
if(data.isCompleted == true) {
this.callbacks[data.Guid](data)
delete this.callbacks[data.Guid]
}
})
this.watch()
}
connect() {
var connection = new signalR.HubConnectionBuilder()
.withUrl("https://gdcmapi-dev.dyndns.info/FileHub", {
accessTokenFactory: () => "Bearer "+SessionStore.user.Authorization
}).configureLogging(signalR.LogLevel.Information)
.build();
connection.on("ReceiveMessage", (message) => {
console.log("ReceiveMessage", message)
})
connection.onreconnected((connectionId) => {
console.assert(connection.state === signalR.HubConnectionState.Connected);
console.log(`Reconnected with connectionId: ${connectionId}`);
});
connection.start()
.then(() => {
console.log("SignalR connection started.");
})
.catch((error) => {
console.error("Error starting SignalR connection:", error);
});
connection.onclose((error) => {
connection.start()
console.log("SignalR connection closed:", error);
});
}
async watch() {
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.watch()
}, 1000)
}
}
subscribe(GUID, callback:Function) {
this.callbacks[GUID] = callback;
}
unsubscribe(GUID) {
delete this.callbacks[GUID]
}
}