import {GameScene} from "./GameScene";
import {
- Connexion,
+ Connection,
GroupCreatedUpdatedMessageInterface,
ListMessageUserPositionInterface,
MessageUserJoined,
MessageUserPositionInterface,
Point,
PointInterface
-} from "../../Connexion";
+} from "../../Connection";
import {SimplePeerInterface, SimplePeer} from "../../WebRtc/SimplePeer";
import {AddPlayerInterface} from "./AddPlayerInterface";
import {ReconnectingSceneName} from "../Reconnecting/ReconnectingScene";
export class GameManager {
status: number;
- private ConnexionInstance: Connexion;
+ private ConnectionInstance: Connection;
private currentGameScene: GameScene;
private playerName: string;
SimplePeer : SimplePeerInterface;
connect(name: string, characterUserSelected : string) {
this.playerName = name;
this.characterUserSelected = characterUserSelected;
- this.ConnexionInstance = new Connexion(this);
- return this.ConnexionInstance.createConnexion(name, characterUserSelected).then((data : any) => {
- this.SimplePeer = new SimplePeer(this.ConnexionInstance);
+ this.ConnectionInstance = new Connection(this);
+ return this.ConnectionInstance.createConnection(name, characterUserSelected).then((data : any) => {
+ this.SimplePeer = new SimplePeer(this.ConnectionInstance);
return data;
}).catch((err) => {
throw err;
}
loadStartMap(){
- return this.ConnexionInstance.loadStartMap().then((data) => {
+ return this.ConnectionInstance.loadStartMap().then((data) => {
return data;
}).catch((err) => {
throw err;
}
joinRoom(sceneKey: string, startX: number, startY: number, direction: string, moving: boolean){
- this.ConnexionInstance.joinARoom(sceneKey, startX, startY, direction, moving);
+ this.ConnectionInstance.joinARoom(sceneKey, startX, startY, direction, moving);
}
onUserJoins(message: MessageUserJoined): void {
}
getPlayerId(): string {
- return this.ConnexionInstance.userId;
+ return this.ConnectionInstance.userId;
}
getCharacterSelected(): string {
}
pushPlayerPosition(event: HasMovedEvent) {
- this.ConnexionInstance.sharePosition(event.x, event.y, event.direction, event.moving);
+ this.ConnectionInstance.sharePosition(event.x, event.y, event.direction, event.moving);
}
loadMap(mapUrl: string, scene: Phaser.Scenes.ScenePlugin, instance: string): string {
-import {ConnexionInterface} from "../Connexion";
+import {ConnectionInterface} from "../Connection";
import {MediaManager} from "./MediaManager";
let Peer = require('simple-peer');
}
export class SimplePeerInterface {}
export class SimplePeer implements SimplePeerInterface{
- private Connexion: ConnexionInterface;
+ private Connection: ConnectionInterface;
private WebRtcRoomId: string;
private Users: Array<UserSimplePear> = new Array<UserSimplePear>();
private MediaManager: MediaManager;
- private PeerConnexionArray: Map<string, any> = new Map<string, any>();
+ private PeerConnectionArray: Map<string, any> = new Map<string, any>();
- constructor(Connexion: ConnexionInterface, WebRtcRoomId: string = "test-webrtc") {
- this.Connexion = Connexion;
+ constructor(Connection: ConnectionInterface, WebRtcRoomId: string = "test-webrtc") {
+ this.Connection = Connection;
this.WebRtcRoomId = WebRtcRoomId;
this.MediaManager = new MediaManager((stream : MediaStream) => {
this.updatedLocalStream();
private initialise() {
//receive signal by gemer
- this.Connexion.receiveWebrtcSignal((message: any) => {
+ this.Connection.receiveWebrtcSignal((message: any) => {
this.receiveWebrtcSignal(message);
});
this.MediaManager.getCamera().then(() => {
//receive message start
- this.Connexion.receiveWebrtcStart((message: any) => {
+ this.Connection.receiveWebrtcStart((message: any) => {
this.receiveWebrtcStart(message);
});
});
//receive signal by gemer
- this.Connexion.disconnectMessage((data: any) => {
- this.closeConnexion(data.userId);
+ this.Connection.disconnectMessage((data: any) => {
+ this.closeConnection(data.userId);
});
}
this.WebRtcRoomId = data.roomId;
this.Users = data.clients;
- //start connexion
+ //start connection
this.startWebRtc();
}
*/
private startWebRtc() {
this.Users.forEach((user: UserSimplePear) => {
- //if it's not an initiator, peer connexion 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){
return;
}
- this.createPeerConnexion(user);
+ this.createPeerConnection(user);
});
}
/**
- * create peer connexion to bind users
+ * create peer connection to bind users
*/
- private createPeerConnexion(user : UserSimplePear) {
- if(this.PeerConnexionArray.has(user.userId)) {
+ private createPeerConnection(user : UserSimplePear) {
+ if(this.PeerConnectionArray.has(user.userId)) {
return;
}
]
},
});
- this.PeerConnexionArray.set(user.userId, peer);
+ this.PeerConnectionArray.set(user.userId, peer);
- //start listen signal for the peer connexion
- this.PeerConnexionArray.get(user.userId).on('signal', (data: any) => {
+ //start listen signal for the peer connection
+ this.PeerConnectionArray.get(user.userId).on('signal', (data: any) => {
this.sendWebrtcSignal(data, user.userId);
});
- this.PeerConnexionArray.get(user.userId).on('stream', (stream: MediaStream) => {
+ this.PeerConnectionArray.get(user.userId).on('stream', (stream: MediaStream) => {
let videoActive = false;
let microphoneActive = false;
stream.getTracks().forEach((track : MediaStreamTrack) => {
this.stream(user.userId, stream);
});
- /*this.PeerConnexionArray.get(user.userId).on('track', (track: MediaStreamTrack, stream: MediaStream) => {
+ /*this.PeerConnectionArray.get(user.userId).on('track', (track: MediaStreamTrack, stream: MediaStream) => {
this.stream(user.userId, stream);
});*/
- this.PeerConnexionArray.get(user.userId).on('close', () => {
- this.closeConnexion(user.userId);
+ this.PeerConnectionArray.get(user.userId).on('close', () => {
+ this.closeConnection(user.userId);
});
- this.PeerConnexionArray.get(user.userId).on('error', (err: any) => {
+ this.PeerConnectionArray.get(user.userId).on('error', (err: any) => {
console.error(`error => ${user.userId} => ${err.code}`, err);
});
- this.PeerConnexionArray.get(user.userId).on('connect', () => {
+ this.PeerConnectionArray.get(user.userId).on('connect', () => {
console.info(`connect => ${user.userId}`);
});
- this.PeerConnexionArray.get(user.userId).on('data', (chunk: Buffer) => {
+ this.PeerConnectionArray.get(user.userId).on('data', (chunk: Buffer) => {
let data = JSON.parse(chunk.toString('utf8'));
if(data.type === "stream"){
this.stream(user.userId, data.stream);
this.addMedia(user.userId);
}
- private closeConnexion(userId : string) {
+ private closeConnection(userId : string) {
try {
this.MediaManager.removeActiveVideo(userId);
- if (!this.PeerConnexionArray.get(userId)) {
+ if (!this.PeerConnectionArray.get(userId)) {
return;
}
// @ts-ignore
- this.PeerConnexionArray.get(userId).destroy();
- this.PeerConnexionArray.delete(userId)
+ this.PeerConnectionArray.get(userId).destroy();
+ this.PeerConnectionArray.delete(userId)
} catch (err) {
- console.error("closeConnexion", err)
+ console.error("closeConnection", err)
}
}
*/
private sendWebrtcSignal(data: any, userId : string) {
try {
- this.Connexion.sendWebrtcSignal(data, this.WebRtcRoomId, null, userId);
+ this.Connection.sendWebrtcSignal(data, this.WebRtcRoomId, null, userId);
}catch (e) {
console.error(`sendWebrtcSignal => ${userId}`, e);
}
private receiveWebrtcSignal(data: any) {
try {
- //if offer type, create peer connexion
+ //if offer type, create peer connection
if(data.signal.type === "offer"){
- this.createPeerConnexion(data);
+ this.createPeerConnection(data);
}
- this.PeerConnexionArray.get(data.userId).signal(data.signal);
+ this.PeerConnectionArray.get(data.userId).signal(data.signal);
} catch (e) {
console.error(`receiveWebrtcSignal => ${data.userId}`, e);
}
let transceiver : any = null;
if(!this.MediaManager.localStream){
//send fake signal
- if(!this.PeerConnexionArray.has(userId)){
+ if(!this.PeerConnectionArray.has(userId)){
return;
}
- this.PeerConnexionArray.get(userId).write(new Buffer(JSON.stringify({
+ this.PeerConnectionArray.get(userId).write(new Buffer(JSON.stringify({
type: "stream",
stream: null
})));
return;
}
this.MediaManager.localStream.getTracks().forEach(
- transceiver = (track: MediaStreamTrack) => this.PeerConnexionArray.get(userId).addTrack(track, this.MediaManager.localStream)
+ transceiver = (track: MediaStreamTrack) => this.PeerConnectionArray.get(userId).addTrack(track, this.MediaManager.localStream)
)
}catch (e) {
console.error(`addMedia => addMedia => ${userId}`, e);