Implemented BroadcastChannelService and FileTransferService
All checks were successful
Setup testing environment and test the code / build (push) Successful in 1m17s
All checks were successful
Setup testing environment and test the code / build (push) Successful in 1m17s
This commit is contained in:
11
src/services/broadcastChannelService.test.ts
Normal file
11
src/services/broadcastChannelService.test.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import {describe, expect, it} from "vitest";
|
||||
import {BroadcastChannelService} from "./broadcastChannelService";
|
||||
|
||||
describe("BroadcastChannelService", () => {
|
||||
const service = new BroadcastChannelService("", "", "", "", "", () => {})
|
||||
|
||||
it("should get stream registry data", async () => {
|
||||
const data = await service.getData()
|
||||
expect(data.status).toBe("broadcasting_starting")
|
||||
})
|
||||
})
|
||||
93
src/services/broadcastChannelService.ts
Normal file
93
src/services/broadcastChannelService.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import {AxiosInstance, isAxiosError} from "axios";
|
||||
import {getClient} from "../core/http";
|
||||
import {MessageListener} from "../domain/websocket.schema";
|
||||
import {WebSocketHandler} from "../core/webSocketHandler";
|
||||
import {AcceptInviteReq} from "../domain/networkService.schema";
|
||||
import {GenericErrorBody} from "../domain/http.schema";
|
||||
import {
|
||||
CreateServerReq,
|
||||
GetRTMPDataReq,
|
||||
JoinWebsocketRoomReq,
|
||||
StreamRegistry
|
||||
} from "../domain/broadcastChannelService.schema";
|
||||
|
||||
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: this.onNewConnId,
|
||||
onNewMessage: wsMessageListener,
|
||||
})
|
||||
}
|
||||
|
||||
private onNewConnId(newConnId: string) {
|
||||
console.log("NetworkService: New connection id")
|
||||
this.client.defaults.headers["X-WS-ID"] = newConnId;
|
||||
}
|
||||
|
||||
async getData(): Promise<StreamRegistry> {
|
||||
try {
|
||||
const resp = await this.client.get<StreamRegistry>(`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<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
async createServer(): Promise<StreamRegistry> {
|
||||
try {
|
||||
const resp = await this.client.post<StreamRegistry>("network/channel/createServer", <CreateServerReq>{
|
||||
userid: this.userid,
|
||||
channelId: this.channelId,
|
||||
networkId: this.networkId,
|
||||
type: "rtmp",
|
||||
categoryId: this.categoryId,
|
||||
});
|
||||
return resp.data
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
async joinWebSocketRoom(): Promise<void> {
|
||||
try {
|
||||
await this.client.post("v2/network/channel/joinWebSocketRoom", <JoinWebsocketRoomReq>{
|
||||
userid: this.userid,
|
||||
channelId: this.channelId,
|
||||
networkId: this.networkId,
|
||||
connId: WebSocketHandler.getInstance().connId,
|
||||
categoryId: this.categoryId,
|
||||
});
|
||||
return
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
}
|
||||
156
src/services/fileTransferService.ts
Normal file
156
src/services/fileTransferService.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import {DatabaseAPI} from "../storage/database";
|
||||
import {AxiosInstance, isAxiosError} from "axios";
|
||||
import {MessageListener} from "../domain/websocket.schema";
|
||||
import {getClient} from "../core/http";
|
||||
import {WebSocketHandler} from "../core/webSocketHandler";
|
||||
import {CreateNetworkReq, Network} from "../domain/networkService.schema";
|
||||
import {GenericErrorBody} from "../domain/http.schema";
|
||||
import {
|
||||
AcceptFileTransferReq, DeclineFileTransferReq, FileTransferSendAnswerRTCReq,
|
||||
FileTransferSendICERTCReq, FileTransferSendOfferRTCReq,
|
||||
StartNewFileTransferReq,
|
||||
StartNewFileTransferResp,
|
||||
TransferableFileMetadata
|
||||
} from "../domain/fileTransferService.schema";
|
||||
|
||||
export class FileTransferService {
|
||||
userid: string;
|
||||
peerId: string;
|
||||
transferId: string = "";
|
||||
client: AxiosInstance
|
||||
|
||||
constructor(userid: string, token: string, peerId: string) {
|
||||
this.userid = userid;
|
||||
this.peerId = peerId;
|
||||
this.client = getClient(false).create({
|
||||
headers: {
|
||||
"Authorization": token,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a new file transfer with the specified user. The new transferId will be saved automatically and not required to be provided later.
|
||||
* @param transferableFiles
|
||||
*/
|
||||
async startNew(transferableFiles: TransferableFileMetadata[]): Promise<string> {
|
||||
try {
|
||||
const resp = await this.client.post<StartNewFileTransferResp>("v2/chat/dm/startNewFileTransfer", <StartNewFileTransferReq>{
|
||||
userid: this.userid,
|
||||
targetUserId: this.peerId,
|
||||
metadata: transferableFiles
|
||||
});
|
||||
this.transferId = resp.data.transferId
|
||||
return resp.data.transferId
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Accepts the file transfer with the specified transferId. The transferId will be saved automatically and not required to be provided later.
|
||||
* @param transferId
|
||||
*/
|
||||
async accept(transferId: string): Promise<RTCConfiguration> {
|
||||
try {
|
||||
const resp = await this.client.post<RTCConfiguration>("v2/chat/dm/acceptFileTransfer", <AcceptFileTransferReq>{
|
||||
userid: this.userid,
|
||||
senderId: this.peerId,
|
||||
transferId: transferId
|
||||
});
|
||||
this.transferId = transferId
|
||||
return resp.data
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Declines the file transfer with the specified transferId.
|
||||
* @param transferId
|
||||
*/
|
||||
async decline(transferId: string): Promise<void> {
|
||||
try {
|
||||
await this.client.post("v2/chat/dm/declineFileTransfer", <DeclineFileTransferReq>{
|
||||
userid: this.userid,
|
||||
senderId: this.peerId,
|
||||
transferId: transferId
|
||||
});
|
||||
return
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards your RTC offer to the specified user.
|
||||
* @param offer
|
||||
*/
|
||||
async sendRtcOffer(offer: string): Promise<void> {
|
||||
try {
|
||||
await this.client.post("v2/chat/dm/sendRtcOfferFileTransfer", <FileTransferSendOfferRTCReq>{
|
||||
userid: this.userid,
|
||||
peerId: this.peerId,
|
||||
transferId: this.transferId,
|
||||
offer: offer,
|
||||
});
|
||||
return
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards your RTC answer to the specified user.
|
||||
* @param answer
|
||||
*/
|
||||
async sendRtcAnswer(answer: string): Promise<void> {
|
||||
try {
|
||||
await this.client.post("v2/chat/dm/sendRtcAnswerFileTransfer", <FileTransferSendAnswerRTCReq>{
|
||||
userid: this.userid,
|
||||
peerId: this.peerId,
|
||||
transferId: this.transferId,
|
||||
answer: answer,
|
||||
});
|
||||
return
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forwards your RTC ICE candidate to the specified user.
|
||||
* @param candidate
|
||||
*/
|
||||
async sendRtcICE(candidate: string): Promise<void> {
|
||||
try {
|
||||
await this.client.post("v2/chat/dm/sendRtcICEFileTransfer", <FileTransferSendICERTCReq>{
|
||||
userid: this.userid,
|
||||
peerId: this.peerId,
|
||||
transferId: this.transferId,
|
||||
candidate: candidate,
|
||||
});
|
||||
return
|
||||
} catch (e) {
|
||||
if (isAxiosError<GenericErrorBody>(e)) {
|
||||
throw e;
|
||||
}
|
||||
throw new Error("Unexpected error")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user