All files / src/utils RTCSignalingHelper.ts

78.46% Statements 51/65
71.42% Branches 10/14
54.54% Functions 12/22
78.46% Lines 51/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                                                                                        25x                   15x 15x 15x     15x     15x     15x     15x         15x 2x 2x   15x 2x 2x   15x 2x 2x   15x 1x 1x   15x           15x                           2x 1x 1x   1x 1x     2x 1x 1x   1x 1x     2x 1x 1x   1x 1x     12x 1x 1x   11x 11x     1x     1x 1x 1x 1x 1x 1x 1x     21x          
import {Socket} from 'socket.io-client';
import {Modal} from 'antd';
/**
 * signaling 의 offer 와 answer 에서 교환되는 데이터 전송객체
 * @param fromClientId offer 나 answer 를 보내는 socketID
 * @param toClientId offer 나 answer 를 받는 socketID
 * @param sdp createOffer 나 createAnswer 로 만들어진 RTCSessionDescriptionInit
 */
export interface OfferAnswerDto {
  fromClientId: string;
  toClientId: string;
  sdp: RTCSessionDescriptionInit;
}
 
/**
 * signaling 의 iceCandidate 과정에서 교환되는 데이터 전송객체
 * @param fromClientId offer 나 answer 를 보내는 socketID
 * @param toClientId offer 나 answer 를 받는 socketID
 * @param ice onIceCadidate 로 만들어진 RTCIceCandidate
 */
export interface IceDto {
  fromClientId: string;
  toClientId: string;
  ice: RTCIceCandidate;
}
 
/**
 * RTCPeerConnection 객체의 signaling 에 사용되는 소켓연결을 책임지는 클레스
 * on 이벤트 : offer, answer, ice, needToOffer(socketIds: string)
 * emit 이벤트 : offer, answer, ice, joinRoom(roomId:string)
 * @param socket backend 에 연결되어있는 socket.io
 * @param isVerbose true 일 경우 log 를 출력함.
 */
class RTCSignalingHelper {
  readonly socket: Socket;
  readonly isVerbose: boolean;
 
  onOffer: (offerDto: OfferAnswerDto) => void;
  onAnswer: (answerDto: OfferAnswerDto) => void;
  onIce: (iceDto: IceDto) => void;
  onNeedToOffer: (socketIDs: string[]) => void;
  onPeerExitRoom: (exitedSocketID: string) => void;
 
  private print(log: string, isError = false): void {
    Iif (this.isVerbose) {
      if (isError) {
        console.error(log);
      } else {
        console.log(log);
      }
    }
  }
 
  constructor(socket: Socket, isVerbose = true) {
    this.socket = socket;
    this.isVerbose = isVerbose;
    this.onOffer = () => {
      this.print('default onOffer called');
    };
    this.onAnswer = () => {
      this.print('default onAnswer called');
    };
    this.onIce = () => {
      this.print('default onIce called');
    };
    this.onNeedToOffer = () => {
      this.print('default onNeedToOffer called');
    };
    this.onPeerExitRoom = () => {
      this.print('default onPeerExitRoom called');
    };
 
    // connect event
    socket.on('offer', (offerDto: OfferAnswerDto) => {
      this.print(`${offerDto.fromClientId} --offer--> ${offerDto.toClientId}`);
      this.onOffer(offerDto);
    });
    socket.on('answer', (ansDto: OfferAnswerDto) => {
      this.print(`${ansDto.fromClientId} --answer--> ${ansDto.toClientId}`);
      this.onAnswer(ansDto);
    });
    socket.on('ice', (iceDto: IceDto) => {
      this.print(`${iceDto.fromClientId} --ice--> ${iceDto.toClientId}`);
      this.onIce(iceDto);
    });
    socket.on('needToOffer', (socketIDs: string[]) => {
      this.print(`you need to offer to ${socketIDs.length - 1} clients`); // 자신이 먼저 조인되고 socketIDs 를 보내기 떄문에 자신이 포함되어있다.
      this.onNeedToOffer(socketIDs);
    });
    socket.on('exitRoom', (exitedSocketID: string) => {
      this.print(`${exitedSocketID} exit room`);
      this.onPeerExitRoom(exitedSocketID);
    });
 
    //default // 추후 밖으로 빼는 것도 나쁘지 않은듯.
    socket.on('disconnect', () => {
      Modal.confirm({
        title: '서버와의 연결이 끊겼습니다.',
        content: '다시 연결 하시겠습니까?',
        onOk: () => {
          window.location.reload();
        },
        onCancel: () => {
          window.location.href = 'https://giggleforest.com/';
        },
      });
    });
  }
  emitOffer(offerDto: OfferAnswerDto): void {
    if (offerDto.fromClientId !== this.socket.id) {
      this.print('emitOffer fromClientId is invalid', true);
      return;
    }
    this.print(`${offerDto.fromClientId} --offer--> ${offerDto.toClientId}`);
    this.socket.emit('offer', offerDto);
  }
  emitAnswer(ansDto: OfferAnswerDto): void {
    if (ansDto.fromClientId !== this.socket.id) {
      this.print('emitAnswer fromClientId is invalid', true);
      return;
    }
    this.print(`${ansDto.fromClientId} --answer--> ${ansDto.toClientId}`);
    this.socket.emit('answer', ansDto);
  }
  emitIce(iceDto: IceDto): void {
    if (iceDto.fromClientId !== this.socket.id) {
      this.print('ice fromClientId is invalid', true);
      return;
    }
    this.print(`${iceDto.fromClientId} --ice--> ${iceDto.toClientId}`);
    this.socket.emit('ice', iceDto);
  }
  joinRoom(roomID: string): void {
    if (roomID === '') {
      this.print('roomID can not be ""', true);
      return;
    }
    this.print(`joinRoom! roomID: ${roomID}, my socketId: ${this.socket.id}`);
    this.socket.emit('joinRoom', roomID);
  }
  close(): void {
    const dummyHandler = () => {
      return;
    };
    this.onAnswer = dummyHandler;
    this.onIce = dummyHandler;
    this.onNeedToOffer = dummyHandler;
    this.onOffer = dummyHandler;
    this.onPeerExitRoom = dummyHandler;
    this.socket.off('disconnect');
    this.socket.close();
  }
  getSocketID(): string {
    return this.socket.id;
  }
}
 
export default RTCSignalingHelper;