More strict typecheck fixes

This commit is contained in:
David Négrier 2020-06-03 22:32:43 +02:00
parent a231024502
commit 111bfcfe8c
3 changed files with 28 additions and 23 deletions

View File

@ -112,7 +112,7 @@ export interface ConnectionInterface {
sharePosition(x: number, y: number, direction: string, moving: boolean): void; sharePosition(x: number, y: number, direction: string, moving: boolean): void;
/*webrtc*/ /*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; 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, { return this.getSocket().emit(EventMessage.WEBRTC_SIGNAL, {
userId: userId ? userId : this.userId, userId: userId ? userId : this.userId,
receiverId: receiverId ? receiverId : this.userId, receiverId: receiverId ? receiverId : this.userId,

View File

@ -9,7 +9,7 @@ import {
Point, Point,
PointInterface PointInterface
} from "../../Connection"; } from "../../Connection";
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer"; import {SimplePeer} from "../../WebRtc/SimplePeer";
import {AddPlayerInterface} from "./AddPlayerInterface"; import {AddPlayerInterface} from "./AddPlayerInterface";
import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene"; import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene";
@ -35,7 +35,7 @@ export class GameManager {
private ConnectionInstance: Connection; private ConnectionInstance: Connection;
private currentGameScene: GameScene; private currentGameScene: GameScene;
private playerName: string; private playerName: string;
SimplePeer : SimplePeerInterface; SimplePeer : SimplePeer;
private characterUserSelected: string; private characterUserSelected: string;
constructor() { constructor() {

View File

@ -1,21 +1,21 @@
import {ConnectionInterface} from "../Connection"; import {ConnectionInterface} from "../Connection";
import {MediaManager} from "./MediaManager"; 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; userId: string;
name?: string; name?: string;
initiator?: boolean; initiator?: boolean;
} }
export class SimplePeerInterface {} export class SimplePeer {
export class SimplePeer implements SimplePeerInterface{
private Connection: ConnectionInterface; private Connection: ConnectionInterface;
private WebRtcRoomId: string; private WebRtcRoomId: string;
private Users: Array<UserSimplePear> = new Array<UserSimplePear>(); private Users: Array<UserSimplePeer> = new Array<UserSimplePeer>();
private MediaManager: MediaManager; private MediaManager: MediaManager;
private PeerConnectionArray: Map<string, any> = new Map<string, any>(); private PeerConnectionArray: Map<string, SimplePeerNamespace.Instance> = new Map<string, SimplePeerNamespace.Instance>();
constructor(Connection: ConnectionInterface, WebRtcRoomId: string = "test-webrtc") { constructor(Connection: ConnectionInterface, WebRtcRoomId: string = "test-webrtc") {
this.Connection = Connection; this.Connection = Connection;
@ -66,7 +66,7 @@ export class SimplePeer implements SimplePeerInterface{
* server has two person connected, start the meet * server has two person connected, start the meet
*/ */
private startWebRtc() { 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 it's not an initiator, peer connection will be created when gamer will receive offer signal
if(!user.initiator){ if(!user.initiator){
return; return;
@ -78,14 +78,14 @@ export class SimplePeer implements SimplePeerInterface{
/** /**
* create peer connection to bind users * create peer connection to bind users
*/ */
private createPeerConnection(user : UserSimplePear) { private createPeerConnection(user : UserSimplePeer) {
if(this.PeerConnectionArray.has(user.userId)) { if(this.PeerConnectionArray.has(user.userId)) {
return; return;
} }
let name = user.name; let name = user.name;
if(!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) { if(userSearch) {
name = userSearch.name; name = userSearch.name;
} }
@ -112,11 +112,11 @@ export class SimplePeer implements SimplePeerInterface{
this.PeerConnectionArray.set(user.userId, peer); this.PeerConnectionArray.set(user.userId, peer);
//start listen signal for the peer connection //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.sendWebrtcSignal(data, user.userId);
}); });
this.PeerConnectionArray.get(user.userId).on('stream', (stream: MediaStream) => { peer.on('stream', (stream: MediaStream) => {
let videoActive = false; let videoActive = false;
let microphoneActive = false; let microphoneActive = false;
stream.getTracks().forEach((track : MediaStreamTrack) => { stream.getTracks().forEach((track : MediaStreamTrack) => {
@ -141,23 +141,23 @@ export class SimplePeer implements SimplePeerInterface{
this.stream(user.userId, stream); 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.stream(user.userId, stream);
});*/ });*/
this.PeerConnectionArray.get(user.userId).on('close', () => { peer.on('close', () => {
this.closeConnection(user.userId); 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); console.error(`error => ${user.userId} => ${err.code}`, err);
}); });
this.PeerConnectionArray.get(user.userId).on('connect', () => { peer.on('connect', () => {
console.info(`connect => ${user.userId}`); 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')); let data = JSON.parse(chunk.toString('utf8'));
if(data.type === "stream"){ if(data.type === "stream"){
this.stream(user.userId, data.stream); this.stream(user.userId, data.stream);
@ -174,7 +174,7 @@ export class SimplePeer implements SimplePeerInterface{
return; return;
} }
// @ts-ignore // @ts-ignore
this.PeerConnectionArray.get(userId).destroy(); this.PeerConnectionArray.get(userId)?.destroy();
this.PeerConnectionArray.delete(userId) this.PeerConnectionArray.delete(userId)
} catch (err) { } catch (err) {
console.error("closeConnection", err) console.error("closeConnection", err)
@ -200,7 +200,12 @@ export class SimplePeer implements SimplePeerInterface{
if(data.signal.type === "offer"){ if(data.signal.type === "offer"){
this.createPeerConnection(data); 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) { } catch (e) {
console.error(`receiveWebrtcSignal => ${data.userId}`, e); console.error(`receiveWebrtcSignal => ${data.userId}`, e);
} }
@ -247,7 +252,7 @@ export class SimplePeer implements SimplePeerInterface{
} }
updatedLocalStream(){ updatedLocalStream(){
this.Users.forEach((user: UserSimplePear) => { this.Users.forEach((user: UserSimplePeer) => {
this.addMedia(user.userId); this.addMedia(user.userId);
}) })
} }