import {AxiosInstance, isAxiosError} from "axios"; import {getClient} from '../core/http.js'; import {MessageListener} from '../domain/websocket.schema.js'; import {WebSocketHandler} from '../core/webSocketHandler.js'; import {AcceptInviteReq} from '../domain/networkService.schema.js'; import {GenericErrorBody} from '../domain/http.schema.js'; import { CreateServerReq, GetRTMPDataReq, JoinWebsocketRoomReq, StreamRegistry } from '../domain/broadcastChannelService.schema.js'; export class BroadcastChannelService { userid: string channelId: string networkId: string categoryId: string client: AxiosInstance; constructor(token: string, userid: string, networkId: string, categoryId: string, channelId: string, wsMessageListener: MessageListener) { this.userid = userid; this.channelId = channelId; this.categoryId = categoryId; this.networkId = networkId; this.client = getClient(false).create({ headers: { "Authorization": token, "X-WS-ID": WebSocketHandler.getInstance().connId } }) WebSocketHandler.getInstance().registerService({ identifier: channelId, onNewConnId: newConnId => this.onNewConnId(newConnId), onNewMessage: wsMessageListener, }) } private onNewConnId(newConnId: string) { console.log("NetworkService: New connection id") this.client.defaults.headers["X-WS-ID"] = newConnId; } async getData(): Promise { try { const resp = await this.client.get(`network/channel/rtmpData?userid=${this.userid}&channelId=${this.channelId}&networkId=${this.networkId}&categoryId=${this.categoryId}`); return resp.data } catch (e) { console.log(e) if (isAxiosError(e)) { throw e; } throw new Error("Unexpected error") } } async createServer(): Promise { try { const resp = await this.client.post("network/channel/createServer", { userid: this.userid, channelId: this.channelId, networkId: this.networkId, type: "rtmp", categoryId: this.categoryId, }); return resp.data } catch (e) { console.log(e) if (isAxiosError(e)) { throw e; } throw new Error("Unexpected error") } } async joinWebSocketRoom(): Promise { try { await this.client.post("v2/network/channel/joinWebSocketRoom", { userid: this.userid, channelId: this.channelId, networkId: this.networkId, connId: WebSocketHandler.getInstance().connId, categoryId: this.categoryId, disableAutoRemove: true }); return } catch (e) { if (isAxiosError(e)) { throw e; } throw new Error("Unexpected error") } } }