Merge world and webrtc conexion
This commit is contained in:
parent
3151113db3
commit
2bfa57b0ba
@ -28,7 +28,7 @@ class App {
|
||||
private config(): void {
|
||||
this.app.use(bodyParser.json());
|
||||
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-Headers", "Origin, X-Requested-With, Content-Type, Accept");
|
||||
next();
|
||||
|
@ -8,6 +8,17 @@ import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_E
|
||||
import {ExtRooms, RefreshUserPositionFunction} from "../Model/Websocket/ExtRoom";
|
||||
import {ExtRoomsInterface} from "../Model/Websocket/ExtRoomsInterface";
|
||||
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{
|
||||
Io: socketIO.Server;
|
||||
@ -31,11 +42,17 @@ export class IoSocketController{
|
||||
|
||||
this.ioConnection();
|
||||
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() {
|
||||
this.Io.on('connection', (socket: Socket) => {
|
||||
this.Io.on(SockerIoEvent.CONNECTION, (socket: Socket) => {
|
||||
/*join-rom event permit to join one room.
|
||||
message :
|
||||
userId : user identification
|
||||
@ -44,10 +61,10 @@ export class IoSocketController{
|
||||
x: user x 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);
|
||||
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
|
||||
@ -64,13 +81,13 @@ export class IoSocketController{
|
||||
rooms.refreshUserPosition = RefreshUserPositionFunction;
|
||||
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);
|
||||
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
|
||||
@ -87,13 +104,53 @@ export class IoSocketController{
|
||||
rooms.refreshUserPosition(rooms, this.Io);
|
||||
});
|
||||
|
||||
socket.on('webrtc-room', (message : string) => {
|
||||
let data = JSON.parse(message);
|
||||
socket.join(data.roomId);
|
||||
(socket as ExSocketInterface).roomId = data.roomId;
|
||||
socket.on(SockerIoEvent.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(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(this.Io.sockets.adapter.rooms[data.roomId].length < 2) {
|
||||
if (this.Io.sockets.adapter.rooms[roomId].length < 2) {
|
||||
return;
|
||||
}
|
||||
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
|
||||
@ -113,24 +170,7 @@ export class IoSocketController{
|
||||
return tabs;
|
||||
}, []);
|
||||
|
||||
client.emit('webrtc-start', JSON.stringify(clientsId));
|
||||
});
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
});
|
||||
client.emit(SockerIoEvent.WEBRTC_START, JSON.stringify({clients: clientsId, roomId: roomId}));
|
||||
});
|
||||
}
|
||||
|
||||
@ -191,8 +231,18 @@ export class IoSocketController{
|
||||
|
||||
//connected user
|
||||
connectedUser(user1 : string, user2 : string){
|
||||
console.log("connectedUser => user1", user1);
|
||||
console.log("connectedUser => user2", user2);
|
||||
/* TODO manager room and group user to enter and leave */
|
||||
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
|
||||
|
@ -4,6 +4,7 @@ import {PointInterface} from "./PointInterface";
|
||||
export interface ExSocketInterface extends Socket {
|
||||
token: any;
|
||||
roomId: string;
|
||||
webRtcRoomId: string;
|
||||
userId: string;
|
||||
position: PointInterface;
|
||||
}
|
@ -3,6 +3,7 @@ import {PointInterface} from "./Websocket/PointInterface";
|
||||
import {Group} from "./Group";
|
||||
import {Distance} from "./Distance";
|
||||
import {UserInterface} from "./UserInterface";
|
||||
import {ExSocketInterface} from "_Model/Websocket/ExSocketInterface";
|
||||
|
||||
export class World {
|
||||
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 {
|
||||
let context = this;
|
||||
let user = this.users.get(userPosition.userId);
|
||||
if(typeof user === 'undefined') {
|
||||
return;
|
||||
|
@ -7,7 +7,6 @@ import {API_URL} from "./Enum/EnvironmentVariable";
|
||||
enum EventMessage{
|
||||
WEBRTC_SIGNAL = "webrtc-signal",
|
||||
WEBRTC_START = "webrtc-start",
|
||||
WEBRTC_ROOM = "webrtc-room",
|
||||
JOIN_ROOM = "join-room",
|
||||
USER_POSITION = "user-position",
|
||||
MESSAGE_ERROR = "message-error"
|
||||
@ -127,8 +126,6 @@ export interface ConnexionInterface {
|
||||
positionOfAllUser(): void;
|
||||
|
||||
/*webrtc*/
|
||||
sendWebrtcRomm(roomId: string): void;
|
||||
|
||||
sendWebrtcSignal(signal: any, roomId: string, userId?: string, receiverId?: string): 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) {
|
||||
this.socket.on(EventMessage.WEBRTC_START, callback);
|
||||
}
|
||||
|
@ -258,7 +258,6 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{
|
||||
//init colision
|
||||
this.physics.add.collider(this.CurrentPlayer, player, (CurrentPlayer: CurrentGamerInterface, MapPlayer: GamerInterface) => {
|
||||
CurrentPlayer.say("Hello, how are you ? ");
|
||||
this.GameManager.SimplePeer.activePhone();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -3,14 +3,13 @@ import {MediaManager} from "./MediaManager";
|
||||
let Peer = require('simple-peer');
|
||||
|
||||
export interface SimplePeerInterface {
|
||||
activePhone(): void;
|
||||
disablePhone(): void;
|
||||
}
|
||||
|
||||
export class SimplePeer {
|
||||
Connexion: ConnexionInterface;
|
||||
MediaManager: MediaManager;
|
||||
RoomId: string;
|
||||
Users: Array<any>;
|
||||
|
||||
PeerConnexionArray: Array<any> = new Array<any>();
|
||||
|
||||
@ -18,12 +17,25 @@ export class SimplePeer {
|
||||
this.Connexion = Connexion;
|
||||
this.RoomId = roomId;
|
||||
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.startWebRtc();
|
||||
this.disablePhone();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* server has two person connected, start the meet
|
||||
*/
|
||||
@ -31,13 +43,9 @@ export class SimplePeer {
|
||||
this.MediaManager.activeVisio();
|
||||
return this.MediaManager.getCamera().then((stream: MediaStream) => {
|
||||
this.MediaManager.localStream = stream;
|
||||
//send message to join a room
|
||||
this.Connexion.sendWebrtcRomm(this.RoomId);
|
||||
|
||||
//receive message start
|
||||
this.Connexion.receiveWebrtcStart((message: string) => {
|
||||
this.receiveWebrtcStart(message);
|
||||
});
|
||||
//create pear connexion
|
||||
this.createPeerConnexion();
|
||||
|
||||
//receive signal by gemer
|
||||
this.Connexion.receiveWebrtcSignal((message: string) => {
|
||||
@ -54,17 +62,16 @@ export class SimplePeer {
|
||||
*/
|
||||
receiveWebrtcStart(message: string) {
|
||||
let data = JSON.parse(message);
|
||||
this.RoomId = data.roomId;
|
||||
this.Users = data.clients;
|
||||
|
||||
//create pear connexion of user stared
|
||||
this.createPeerConnexion(data);
|
||||
//active button for player
|
||||
this.activePhone();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param users
|
||||
*/
|
||||
createPeerConnexion(users : Array<any>) {
|
||||
users.forEach((user: any) => {
|
||||
|
||||
createPeerConnexion() {
|
||||
this.Users.forEach((user: any) => {
|
||||
if(this.PeerConnexionArray[user.userId]){
|
||||
return;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user