Merge world and webrtc conexion
This commit is contained in:
parent
3151113db3
commit
2bfa57b0ba
@ -28,7 +28,7 @@ class App {
|
|||||||
private config(): void {
|
private config(): void {
|
||||||
this.app.use(bodyParser.json());
|
this.app.use(bodyParser.json());
|
||||||
this.app.use(bodyParser.urlencoded({extended: false}));
|
this.app.use(bodyParser.urlencoded({extended: false}));
|
||||||
this.app.use(function (req: Request, res: Response, next) {
|
this.app.use((req: Request, res: Response, next) => {
|
||||||
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
|
res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
|
||||||
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||||
next();
|
next();
|
||||||
|
@ -8,6 +8,17 @@ import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_E
|
|||||||
import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom";
|
import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom";
|
||||||
import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
|
import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
|
||||||
import {World} from "../Model/World";
|
import {World} from "../Model/World";
|
||||||
|
import { uuid } from 'uuidv4';
|
||||||
|
|
||||||
|
enum SockerIoEvent {
|
||||||
|
CONNECTION = "connection",
|
||||||
|
DISCONNECTION = "disconnect",
|
||||||
|
JOIN_ROOM = "join-room",
|
||||||
|
USER_POSITION = "user-position",
|
||||||
|
WEBRTC_SIGNAL = "webrtc-signal",
|
||||||
|
WEBRTC_START = "webrtc-start",
|
||||||
|
MESSAGE_ERROR = "message-error",
|
||||||
|
}
|
||||||
|
|
||||||
export class IoSocketController{
|
export class IoSocketController{
|
||||||
Io: socketIO.Server;
|
Io: socketIO.Server;
|
||||||
@ -31,11 +42,17 @@ export class IoSocketController{
|
|||||||
|
|
||||||
this.ioConnection();
|
this.ioConnection();
|
||||||
this.shareUsersPosition();
|
this.shareUsersPosition();
|
||||||
this.World = new World(this.connectedUser, this.disConnectedUser);
|
|
||||||
|
//don't send only function because the context will be not this
|
||||||
|
this.World = new World((user1 : string, user2 : string) => {
|
||||||
|
this.connectedUser(user1, user2);
|
||||||
|
}, (user1 : string, user2 : string) => {
|
||||||
|
this.disConnectedUser(user1, user2);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
ioConnection() {
|
ioConnection() {
|
||||||
this.Io.on('connection', (socket: Socket) => {
|
this.Io.on(SockerIoEvent.CONNECTION, (socket: Socket) => {
|
||||||
/*join-rom event permit to join one room.
|
/*join-rom event permit to join one room.
|
||||||
message :
|
message :
|
||||||
userId : user identification
|
userId : user identification
|
||||||
@ -44,10 +61,10 @@ export class IoSocketController{
|
|||||||
x: user x position on map
|
x: user x position on map
|
||||||
y: user y position on map
|
y: user y position on map
|
||||||
*/
|
*/
|
||||||
socket.on('join-room', (message : string) => {
|
socket.on(SockerIoEvent.JOIN_ROOM, (message : string) => {
|
||||||
let messageUserPosition = this.hydrateMessageReceive(message);
|
let messageUserPosition = this.hydrateMessageReceive(message);
|
||||||
if(messageUserPosition instanceof Error){
|
if(messageUserPosition instanceof Error){
|
||||||
return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message}))
|
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}))
|
||||||
}
|
}
|
||||||
|
|
||||||
//join user in room
|
//join user in room
|
||||||
@ -64,13 +81,13 @@ export class IoSocketController{
|
|||||||
rooms.refreshUserPosition = RefreshUserPositionFunction;
|
rooms.refreshUserPosition = RefreshUserPositionFunction;
|
||||||
rooms.refreshUserPosition(rooms, this.Io);
|
rooms.refreshUserPosition(rooms, this.Io);
|
||||||
|
|
||||||
socket.to(messageUserPosition.roomId).emit('join-room', messageUserPosition.toString());
|
socket.to(messageUserPosition.roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserPosition.toString());
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('user-position', (message : string) => {
|
socket.on(SockerIoEvent.USER_POSITION, (message : string) => {
|
||||||
let messageUserPosition = this.hydrateMessageReceive(message);
|
let messageUserPosition = this.hydrateMessageReceive(message);
|
||||||
if (messageUserPosition instanceof Error) {
|
if (messageUserPosition instanceof Error) {
|
||||||
return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message}));
|
return socket.emit(SockerIoEvent.MESSAGE_ERROR, JSON.stringify({message: messageUserPosition.message}));
|
||||||
}
|
}
|
||||||
|
|
||||||
// update position in the worl
|
// update position in the worl
|
||||||
@ -87,13 +104,53 @@ export class IoSocketController{
|
|||||||
rooms.refreshUserPosition(rooms, this.Io);
|
rooms.refreshUserPosition(rooms, this.Io);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('webrtc-room', (message : string) => {
|
socket.on(SockerIoEvent.WEBRTC_SIGNAL, (message : string) => {
|
||||||
let data = JSON.parse(message);
|
let data : any = JSON.parse(message);
|
||||||
socket.join(data.roomId);
|
|
||||||
(socket as ExSocketInterface).roomId = data.roomId;
|
|
||||||
|
|
||||||
|
//send only at user
|
||||||
|
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
|
||||||
|
for(let i = 0; i < clients.length; i++){
|
||||||
|
let client : ExSocketInterface = clients[i];
|
||||||
|
if(client.userId !== data.receiverId){
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
client.emit(SockerIoEvent.WEBRTC_SIGNAL, message);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
socket.on(SockerIoEvent.DISCONNECTION, (reason : string) => {
|
||||||
|
let Client = (socket as ExSocketInterface);
|
||||||
|
//leave group of user
|
||||||
|
this.World.leave(Client);
|
||||||
|
|
||||||
|
//leave room
|
||||||
|
socket.leave(Client.roomId);
|
||||||
|
socket.leave(Client.webRtcRoomId);
|
||||||
|
|
||||||
|
//delete all socket information
|
||||||
|
delete Client.userId;
|
||||||
|
delete Client.webRtcRoomId;
|
||||||
|
delete Client.roomId;
|
||||||
|
delete Client.token;
|
||||||
|
delete Client.position;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param socket
|
||||||
|
* @param roomId
|
||||||
|
*/
|
||||||
|
joinWebRtcRoom(socket : ExSocketInterface, roomId : string) {
|
||||||
|
if(socket.webRtcRoomId === roomId){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
socket.join(roomId);
|
||||||
|
socket.webRtcRoomId = roomId;
|
||||||
//if two persone in room share
|
//if two persone in room share
|
||||||
if(this.Io.sockets.adapter.rooms[data.roomId].length < 2) {
|
if (this.Io.sockets.adapter.rooms[roomId].length < 2) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
|
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
|
||||||
@ -113,24 +170,7 @@ export class IoSocketController{
|
|||||||
return tabs;
|
return tabs;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
client.emit('webrtc-start', JSON.stringify(clientsId));
|
client.emit(SockerIoEvent.WEBRTC_START, JSON.stringify({clients: clientsId, roomId: roomId}));
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
socket.on('webrtc-signal', (message : string) => {
|
|
||||||
let data : any = JSON.parse(message);
|
|
||||||
|
|
||||||
//send only at user
|
|
||||||
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
|
|
||||||
for(let i = 0; i < clients.length; i++){
|
|
||||||
let client : ExSocketInterface = clients[i];
|
|
||||||
if(client.userId !== data.receiverId){
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
client.emit('webrtc-signal', message);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,8 +231,18 @@ export class IoSocketController{
|
|||||||
|
|
||||||
//connected user
|
//connected user
|
||||||
connectedUser(user1 : string, user2 : string){
|
connectedUser(user1 : string, user2 : string){
|
||||||
console.log("connectedUser => user1", user1);
|
/* TODO manager room and group user to enter and leave */
|
||||||
console.log("connectedUser => user2", user2);
|
let roomId = uuid();
|
||||||
|
let clients : Array<any> = Object.values(this.Io.sockets.sockets);
|
||||||
|
let User1 = clients.find((user : ExSocketInterface) => user.userId === user1);
|
||||||
|
let User2 = clients.find((user : ExSocketInterface) => user.userId === user2);
|
||||||
|
|
||||||
|
if(User1) {
|
||||||
|
this.joinWebRtcRoom(User1, roomId);
|
||||||
|
}
|
||||||
|
if(User2) {
|
||||||
|
this.joinWebRtcRoom(User2, roomId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//connected user
|
//connected user
|
||||||
|
@ -4,6 +4,7 @@ import {PointInterface} from "./PointInterface";
|
|||||||
export interface ExSocketInterface extends Socket {
|
export interface ExSocketInterface extends Socket {
|
||||||
token: any;
|
token: any;
|
||||||
roomId: string;
|
roomId: string;
|
||||||
|
webRtcRoomId: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
position: PointInterface;
|
position: PointInterface;
|
||||||
}
|
}
|
@ -3,6 +3,7 @@ import {PointInterface} from "./Websocket/PointInterface";
|
|||||||
import {Group} from "./Group";
|
import {Group} from "./Group";
|
||||||
import {Distance} from "./Distance";
|
import {Distance} from "./Distance";
|
||||||
import {UserInterface} from "./UserInterface";
|
import {UserInterface} from "./UserInterface";
|
||||||
|
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||||
|
|
||||||
export class World {
|
export class World {
|
||||||
static readonly MIN_DISTANCE = 160;
|
static readonly MIN_DISTANCE = 160;
|
||||||
@ -29,8 +30,12 @@ export class World {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public leave(user : ExSocketInterface){
|
||||||
|
/*TODO leaver user in group*/
|
||||||
|
this.users.delete(user.userId);
|
||||||
|
}
|
||||||
|
|
||||||
public updatePosition(userPosition: MessageUserPosition): void {
|
public updatePosition(userPosition: MessageUserPosition): void {
|
||||||
let context = this;
|
|
||||||
let user = this.users.get(userPosition.userId);
|
let user = this.users.get(userPosition.userId);
|
||||||
if(typeof user === 'undefined') {
|
if(typeof user === 'undefined') {
|
||||||
return;
|
return;
|
||||||
|
@ -7,7 +7,6 @@ import {API_URL} from "./Enum/EnvironmentVariable";
|
|||||||
enum EventMessage{
|
enum EventMessage{
|
||||||
WEBRTC_SIGNAL = "webrtc-signal",
|
WEBRTC_SIGNAL = "webrtc-signal",
|
||||||
WEBRTC_START = "webrtc-start",
|
WEBRTC_START = "webrtc-start",
|
||||||
WEBRTC_ROOM = "webrtc-room",
|
|
||||||
JOIN_ROOM = "join-room",
|
JOIN_ROOM = "join-room",
|
||||||
USER_POSITION = "user-position",
|
USER_POSITION = "user-position",
|
||||||
MESSAGE_ERROR = "message-error"
|
MESSAGE_ERROR = "message-error"
|
||||||
@ -127,8 +126,6 @@ export interface ConnexionInterface {
|
|||||||
positionOfAllUser(): void;
|
positionOfAllUser(): void;
|
||||||
|
|
||||||
/*webrtc*/
|
/*webrtc*/
|
||||||
sendWebrtcRomm(roomId: string): void;
|
|
||||||
|
|
||||||
sendWebrtcSignal(signal: any, roomId: string, userId?: string, receiverId?: string): void;
|
sendWebrtcSignal(signal: any, roomId: string, userId?: string, receiverId?: string): void;
|
||||||
|
|
||||||
receiveWebrtcSignal(callBack: Function): void;
|
receiveWebrtcSignal(callBack: Function): void;
|
||||||
@ -239,10 +236,6 @@ export class Connexion implements ConnexionInterface {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
sendWebrtcRomm(roomId: string) {
|
|
||||||
this.socket.emit(EventMessage.WEBRTC_ROOM, JSON.stringify({roomId: roomId}));
|
|
||||||
}
|
|
||||||
|
|
||||||
receiveWebrtcStart(callback: Function) {
|
receiveWebrtcStart(callback: Function) {
|
||||||
this.socket.on(EventMessage.WEBRTC_START, callback);
|
this.socket.on(EventMessage.WEBRTC_START, callback);
|
||||||
}
|
}
|
||||||
|
@ -258,7 +258,6 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
|||||||
//init colision
|
//init colision
|
||||||
this.physics.add.collider(this.CurrentPlayer, player, (CurrentPlayer: CurrentGamerInterface, MapPlayer: GamerInterface) => {
|
this.physics.add.collider(this.CurrentPlayer, player, (CurrentPlayer: CurrentGamerInterface, MapPlayer: GamerInterface) => {
|
||||||
CurrentPlayer.say("Hello, how are you ? ");
|
CurrentPlayer.say("Hello, how are you ? ");
|
||||||
this.GameManager.SimplePeer.activePhone();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,13 @@ import {MediaManager} from "./MediaManager";
|
|||||||
let Peer = require('simple-peer');
|
let Peer = require('simple-peer');
|
||||||
|
|
||||||
export interface SimplePeerInterface {
|
export interface SimplePeerInterface {
|
||||||
activePhone(): void;
|
|
||||||
disablePhone(): void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SimplePeer {
|
export class SimplePeer {
|
||||||
Connexion: ConnexionInterface;
|
Connexion: ConnexionInterface;
|
||||||
MediaManager: MediaManager;
|
MediaManager: MediaManager;
|
||||||
RoomId: string;
|
RoomId: string;
|
||||||
|
Users: Array<any>;
|
||||||
|
|
||||||
PeerConnexionArray: Array<any> = new Array<any>();
|
PeerConnexionArray: Array<any> = new Array<any>();
|
||||||
|
|
||||||
@ -18,12 +17,25 @@ export class SimplePeer {
|
|||||||
this.Connexion = Connexion;
|
this.Connexion = Connexion;
|
||||||
this.RoomId = roomId;
|
this.RoomId = roomId;
|
||||||
this.MediaManager = new MediaManager();
|
this.MediaManager = new MediaManager();
|
||||||
|
this.initialise();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* permit to listen when user could start visio
|
||||||
|
*/
|
||||||
|
private initialise(){
|
||||||
|
|
||||||
|
//receive message start
|
||||||
|
this.Connexion.receiveWebrtcStart((message: string) => {
|
||||||
|
this.receiveWebrtcStart(message);
|
||||||
|
});
|
||||||
|
|
||||||
|
//when button to call is clicked, start video
|
||||||
this.MediaManager.getElementActivePhone().addEventListener("click", () => {
|
this.MediaManager.getElementActivePhone().addEventListener("click", () => {
|
||||||
this.startWebRtc();
|
this.startWebRtc();
|
||||||
this.disablePhone();
|
this.disablePhone();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* server has two person connected, start the meet
|
* server has two person connected, start the meet
|
||||||
*/
|
*/
|
||||||
@ -31,13 +43,9 @@ export class SimplePeer {
|
|||||||
this.MediaManager.activeVisio();
|
this.MediaManager.activeVisio();
|
||||||
return this.MediaManager.getCamera().then((stream: MediaStream) => {
|
return this.MediaManager.getCamera().then((stream: MediaStream) => {
|
||||||
this.MediaManager.localStream = stream;
|
this.MediaManager.localStream = stream;
|
||||||
//send message to join a room
|
|
||||||
this.Connexion.sendWebrtcRomm(this.RoomId);
|
|
||||||
|
|
||||||
//receive message start
|
//create pear connexion
|
||||||
this.Connexion.receiveWebrtcStart((message: string) => {
|
this.createPeerConnexion();
|
||||||
this.receiveWebrtcStart(message);
|
|
||||||
});
|
|
||||||
|
|
||||||
//receive signal by gemer
|
//receive signal by gemer
|
||||||
this.Connexion.receiveWebrtcSignal((message: string) => {
|
this.Connexion.receiveWebrtcSignal((message: string) => {
|
||||||
@ -54,17 +62,16 @@ export class SimplePeer {
|
|||||||
*/
|
*/
|
||||||
receiveWebrtcStart(message: string) {
|
receiveWebrtcStart(message: string) {
|
||||||
let data = JSON.parse(message);
|
let data = JSON.parse(message);
|
||||||
|
this.RoomId = data.roomId;
|
||||||
|
this.Users = data.clients;
|
||||||
|
|
||||||
//create pear connexion of user stared
|
//active button for player
|
||||||
this.createPeerConnexion(data);
|
this.activePhone();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
createPeerConnexion() {
|
||||||
* @param users
|
this.Users.forEach((user: any) => {
|
||||||
*/
|
|
||||||
createPeerConnexion(users : Array<any>) {
|
|
||||||
users.forEach((user: any) => {
|
|
||||||
if(this.PeerConnexionArray[user.userId]){
|
if(this.PeerConnexionArray[user.userId]){
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user