From 111bfcfe8ca3bc2bf2a69de6855b1f422a7bcfeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20N=C3=A9grier?= Date: Wed, 3 Jun 2020 22:32:43 +0200 Subject: [PATCH] More strict typecheck fixes --- front/src/Connection.ts | 4 +-- front/src/Phaser/Game/GameManager.ts | 4 +-- front/src/WebRtc/SimplePeer.ts | 43 ++++++++++++++++------------ 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/front/src/Connection.ts b/front/src/Connection.ts index 08ffb871..cf7a3856 100644 --- a/front/src/Connection.ts +++ b/front/src/Connection.ts @@ -112,7 +112,7 @@ export interface ConnectionInterface { sharePosition(x: number, y: number, direction: string, moving: boolean): void; /*webrtc*/ - sendWebrtcSignal(signal: any, roomId: string, userId?: string, receiverId?: string): void; + sendWebrtcSignal(signal: any, roomId: string, userId?: string|null, receiverId?: string): void; receiveWebrtcSignal(callBack: Function): void; @@ -268,7 +268,7 @@ export class Connection implements ConnectionInterface { }) } - sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) { + sendWebrtcSignal(signal: any, roomId: string, userId? : string|null, receiverId? : string) { return this.getSocket().emit(EventMessage.WEBRTC_SIGNAL, { userId: userId ? userId : this.userId, receiverId: receiverId ? receiverId : this.userId, diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 7f895048..39ed4bd7 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -9,7 +9,7 @@ import { Point, PointInterface } from "../../Connection"; -import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer"; +import {SimplePeer} from "../../WebRtc/SimplePeer"; import {AddPlayerInterface} from "./AddPlayerInterface"; import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene"; @@ -35,7 +35,7 @@ export class GameManager { private ConnectionInstance: Connection; private currentGameScene: GameScene; private playerName: string; - SimplePeer : SimplePeerInterface; + SimplePeer : SimplePeer; private characterUserSelected: string; constructor() { diff --git a/front/src/WebRtc/SimplePeer.ts b/front/src/WebRtc/SimplePeer.ts index 3693924d..68925e9c 100644 --- a/front/src/WebRtc/SimplePeer.ts +++ b/front/src/WebRtc/SimplePeer.ts @@ -1,21 +1,21 @@ import {ConnectionInterface} from "../Connection"; import {MediaManager} from "./MediaManager"; -let Peer = require('simple-peer'); +import * as SimplePeerNamespace from "simple-peer"; +let Peer: SimplePeerNamespace.SimplePeer = require('simple-peer'); -class UserSimplePear{ +class UserSimplePeer{ userId: string; name?: string; initiator?: boolean; } -export class SimplePeerInterface {} -export class SimplePeer implements SimplePeerInterface{ +export class SimplePeer { private Connection: ConnectionInterface; private WebRtcRoomId: string; - private Users: Array = new Array(); + private Users: Array = new Array(); private MediaManager: MediaManager; - private PeerConnectionArray: Map = new Map(); + private PeerConnectionArray: Map = new Map(); constructor(Connection: ConnectionInterface, WebRtcRoomId: string = "test-webrtc") { this.Connection = Connection; @@ -66,7 +66,7 @@ export class SimplePeer implements SimplePeerInterface{ * server has two person connected, start the meet */ private startWebRtc() { - this.Users.forEach((user: UserSimplePear) => { + this.Users.forEach((user: UserSimplePeer) => { //if it's not an initiator, peer connection will be created when gamer will receive offer signal if(!user.initiator){ return; @@ -78,14 +78,14 @@ export class SimplePeer implements SimplePeerInterface{ /** * create peer connection to bind users */ - private createPeerConnection(user : UserSimplePear) { + private createPeerConnection(user : UserSimplePeer) { if(this.PeerConnectionArray.has(user.userId)) { return; } let name = user.name; if(!name){ - let userSearch = this.Users.find((userSearch: UserSimplePear) => userSearch.userId === user.userId); + let userSearch = this.Users.find((userSearch: UserSimplePeer) => userSearch.userId === user.userId); if(userSearch) { name = userSearch.name; } @@ -112,11 +112,11 @@ export class SimplePeer implements SimplePeerInterface{ this.PeerConnectionArray.set(user.userId, peer); //start listen signal for the peer connection - this.PeerConnectionArray.get(user.userId).on('signal', (data: any) => { + peer.on('signal', (data: any) => { this.sendWebrtcSignal(data, user.userId); }); - this.PeerConnectionArray.get(user.userId).on('stream', (stream: MediaStream) => { + peer.on('stream', (stream: MediaStream) => { let videoActive = false; let microphoneActive = false; stream.getTracks().forEach((track : MediaStreamTrack) => { @@ -141,23 +141,23 @@ export class SimplePeer implements SimplePeerInterface{ this.stream(user.userId, stream); }); - /*this.PeerConnectionArray.get(user.userId).on('track', (track: MediaStreamTrack, stream: MediaStream) => { + /*peer.on('track', (track: MediaStreamTrack, stream: MediaStream) => { this.stream(user.userId, stream); });*/ - this.PeerConnectionArray.get(user.userId).on('close', () => { + peer.on('close', () => { this.closeConnection(user.userId); }); - this.PeerConnectionArray.get(user.userId).on('error', (err: any) => { + peer.on('error', (err: any) => { console.error(`error => ${user.userId} => ${err.code}`, err); }); - this.PeerConnectionArray.get(user.userId).on('connect', () => { + peer.on('connect', () => { console.info(`connect => ${user.userId}`); }); - this.PeerConnectionArray.get(user.userId).on('data', (chunk: Buffer) => { + peer.on('data', (chunk: Buffer) => { let data = JSON.parse(chunk.toString('utf8')); if(data.type === "stream"){ this.stream(user.userId, data.stream); @@ -174,7 +174,7 @@ export class SimplePeer implements SimplePeerInterface{ return; } // @ts-ignore - this.PeerConnectionArray.get(userId).destroy(); + this.PeerConnectionArray.get(userId)?.destroy(); this.PeerConnectionArray.delete(userId) } catch (err) { console.error("closeConnection", err) @@ -200,7 +200,12 @@ export class SimplePeer implements SimplePeerInterface{ if(data.signal.type === "offer"){ this.createPeerConnection(data); } - this.PeerConnectionArray.get(data.userId).signal(data.signal); + let peer = this.PeerConnectionArray.get(data.userId); + if (peer !== undefined) { + peer.signal(data.signal); + } else { + console.error('Could not find peer whose ID is "'+data.userId+'" in PeerConnectionArray'); + } } catch (e) { console.error(`receiveWebrtcSignal => ${data.userId}`, e); } @@ -247,7 +252,7 @@ export class SimplePeer implements SimplePeerInterface{ } updatedLocalStream(){ - this.Users.forEach((user: UserSimplePear) => { + this.Users.forEach((user: UserSimplePeer) => { this.addMedia(user.userId); }) }