Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { signal } from '@angular/core';
- import { websocketDef } from 'src/interfaces/interfaces';
- import { webSocket } from 'rxjs/webSocket';
- import { environment } from 'src/environments/environment.development';
- export const Chats = signal<ChatService[]>([]);
- const ws = webSocket(`${environment.websocket_url}/chat/ws`)
- export class ChatService {
- chatdata: Chat;
- peerconnections: PeerConnection[] = [];
- userid: string;
- peerid = Math.floor(Math.random() * 9999)
- constructor(chatdata: Chat, userid: string) {
- console.log(chatdata)
- this.chatdata = chatdata;
- this.userid = userid;
- // Make user available
- ws.next({
- action: "makeUserAvailable",
- value: JSON.stringify({
- chatid: chatdata.chatid,
- peerid: this.peerid,
- })
- })
- ws.subscribe(
- (message) => {
- let action = (message as websocketDef).action
- switch (action) {
- case "peerids":
- let data = message as { peerids: number[] }
- data.peerids.forEach((peerid) => {
- this.peerconnections.push(new PeerConnection(peerid, this.peerid, this.chatdata.chatid, true, null))
- })
- break;
- case "chatOffer":
- console.log("Got offer")
- let offerData = message as offer;
- let chat = this.peerconnections.find((conn) => conn.peerid == offerData.peerid);
- if (chat && !this.amIPolite(this.peerid, offerData.peerid) && chat.status == "connected") {
- return;
- }
- let peerChat = new PeerConnection(offerData.peerid, this.peerid, this.chatdata.chatid, false, offerData)
- if (!chat) {
- this.peerconnections.push(peerChat);
- }
- break;
- case "chatAnswer":
- let answerData = message as answer;
- let chatToApplyAnswer = this.peerconnections.find((conn) => conn.peerid == answerData.peerid);
- if (chatToApplyAnswer) {
- chatToApplyAnswer.applyAnswer(answerData)
- }
- }
- }
- )
- }
- amIPolite(peerid: number, remotePeerid: number): boolean {
- return peerid > remotePeerid;
- }
- }
- class PeerConnection {
- config = {
- iceServers: [
- {
- urls: 'stun:stun.l.google.com:19302',
- },
- {
- urls: 'turn:213.181.206.131:3478',
- username: 'chatenium',
- credential: 'GeraErika01',
- },
- ],
- };
- status: "initializing" | "connecting" | "connected" | "failed" = "initializing";
- error = "";
- peerconnection = new RTCPeerConnection(this.config);
- chatid: string;
- datachannel: RTCDataChannel | null = null;
- myPeerid: number;
- peerid: number;
- constructor(peerid: number, myPeerid: number, chatid: string, offerer: boolean, offerData: offer | null) {
- this.myPeerid = myPeerid;
- this.peerid = peerid;
- this.chatid = chatid;
- // Connect peers
- if (offerer) {
- this.connect();
- } else {
- if (!offerData) {
- return
- }
- this.answer(offerData);
- }
- this.peerconnection.ondatachannel = e => {
- console.log("Got datachannel!")
- }
- this.peerconnection.onicecandidate = e => {
- console.log("Ice")
- }
- this.peerconnection.onconnectionstatechange = e => {
- console.log(e)
- }
- }
- async connect() {
- let datachannel = this.peerconnection.createDataChannel("DmMessage");
- datachannel.onopen = e => {
- console.log("Offer: DataChannel Open")
- }
- this.datachannel = datachannel;
- let offer = await this.peerconnection.createOffer()
- await this.peerconnection.setLocalDescription(offer)
- ws.next({
- action: "chatOffer",
- value: JSON.stringify({
- peerid: this.myPeerid,
- offer: JSON.stringify(offer),
- action: "chatOffer",
- chatid: this.chatid,
- })
- })
- // Listen //
- setTimeout(() => {
- if (this.status != "connected") {
- this.status = "failed";
- this.error = "timeout"
- }
- }, 10000);
- }
- async answer(offerData: offer) {
- console.log("Answer applied")
- this.peerconnection = new RTCPeerConnection(this.config)
- await this.peerconnection.setRemoteDescription(JSON.parse(offerData.offer))
- let answer = await this.peerconnection.createAnswer();
- await this.peerconnection.setLocalDescription(answer);
- ws.next({
- action: 'chatAnswer',
- value: JSON.stringify({
- peerid: this.myPeerid,
- chatid: this.chatid,
- answer: JSON.stringify(answer),
- action: 'chatAnswer',
- }),
- });
- }
- async applyAnswer(data: answer) {
- await this.peerconnection.setRemoteDescription(JSON.parse(data.answer))
- }
- }
- export interface Chat {
- chatid: string;
- displayName: string;
- notif: number;
- pfp: string;
- userid: string;
- username: string;
- }
- interface offer {
- chatid: string;
- offer: string;
- peerid: number;
- }
- interface answer {
- answer: string;
- peerid: number;
- }
- interface iceCandData {
- peerid: number;
- action: string;
- icecandidate: string;
- chatid: string;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement