Create Room Self Session #8

This commit is contained in:
Abdussamed 2023-07-06 20:37:55 +03:00
parent 202fc9af23
commit d414760bf4
3 changed files with 169 additions and 11 deletions

View File

@ -67,6 +67,11 @@ function Room()
*/ */
this.waitingInvited = new Set(); this.waitingInvited = new Set();
/**
* @type {Map<string,any>}
*/
this.info = new Map();
this.sync = function(...args){ this.sync = function(...args){
process.nextTick(()=>{ process.nextTick(()=>{
for (const name of args) { for (const name of args) {
@ -117,6 +122,14 @@ Room.prototype.toJSON = function(detailed){
} }
return obj; return obj;
}; };
Room.prototype.getInfo = function(){
let obj = {};
for (const [name, value] of this.info)
{
obj[name] = value;
}
return obj;
};
/** /**
* @param {Object} data * @param {Object} data
* @param {Room} room * @param {Room} room
@ -244,7 +257,8 @@ let CreateRoomVerify = joi.object({
description: joi.string().required(), description: joi.string().required(),
name: joi.string().required(), name: joi.string().required(),
credential: joi.string().optional(), credential: joi.string().optional(),
ifexistsJoin: joi.boolean().optional() ifexistsJoin: joi.boolean().optional(),
autoFetchInfo: joi.boolean().optional(),
}); });
addService(({ addService(({
@ -412,6 +426,11 @@ addService(({
{ {
if(room.credential == Sha256(message.credential + "")) if(room.credential == Sha256(message.credential + ""))
{ {
let info = {};
if(autoFetchInfo)
{
info.info = room.getInfo();
};
room.join(client); room.join(client);
return end({ return end({
status : "success", status : "success",
@ -423,10 +442,16 @@ addService(({
area: "credential" area: "credential"
}) })
}else if(room.joinType == "free"){ }else if(room.joinType == "free"){
let info = {};
if(autoFetchInfo)
{
info.info = room.getInfo();
};
room.join(client); room.join(client);
return end({ return end({
status : "success", status : "success",
room: room.toJSON() room: room.toJSON(),
...info
}) })
}else if(room.joinType == "invite"){ }else if(room.joinType == "invite"){
room.waitingInvited.add(client.id); room.waitingInvited.add(client.id);
@ -597,19 +622,94 @@ addService(({
} }
case 'room/list':{ case 'room/list':{
let rooms = []; let rooms = [];
for (const [id, {accessType,name,joinType,description}] of Room.rooms) if(accessType == "public"){ for (const [id, {accessType,name,joinType,description}] of Room.rooms)
rooms.push({ {
name, if(accessType == "public")
joinType, {
description, rooms.push({
id name,
}) joinType,
description,
id
})
}
} }
end({ end({
type:'public/rooms', type:'public/rooms',
rooms rooms
}); });
} }
case 'room/info':{
let {roomId,name} = message;
// Odanın varlığının kontrolü
if(!Room.rooms.has(roomId))
{
end({
status : "fail",
message : "NOT-FOUND-ROOM"
})
};
let room = Room.rooms.get(roomId);
// erişim kontrolü
if(!client.rooms.has(room.id))
{
return end({
status : "fail",
message : "NO-jıoned-ROOM"
})
}
if(name)
{
return end({
status: "success",
value: room.info.get(name)
});
}else{
return end({
status: "success",
value: room.getInfo()
});
}
}
case 'room/setinfo':{
let {roomId,name,value} = message;
// Odanın varlığının kontrolü
if(!Room.rooms.has(roomId))
{
end({
status : "fail",
message : "NOT-FOUND-ROOM"
})
};
let room = Room.rooms.get(roomId);
// erişim kontrolü
if(!client.rooms.has(room.id))
{
return end({
status : "fail",
message : "NO-jıoned-ROOM"
})
}
room.info.set(name, value);
for (const [,peer] of room.clients)
{
peer.send([{
from: client.id,
name,
value
},"room/info"]);
}
return end({
status: "success"
});
}
default:{ default:{
next(); next();
} }

View File

@ -1,6 +1,7 @@
import EventTarget from "./EventTarget"; import EventTarget from "./EventTarget";
import MWSE from "./index"; import MWSE from "./index";
import Peer from "./Peer"; import Peer from "./Peer";
import { RoomInfo } from "./RoomInfo";
export interface IRoomOptions export interface IRoomOptions
{ {
@ -13,6 +14,7 @@ export interface IRoomOptions
notifyActionInvite?: boolean; notifyActionInvite?: boolean;
notifyActionJoined?: boolean; notifyActionJoined?: boolean;
notifyActionEjected?: boolean; notifyActionEjected?: boolean;
autoFetchInfo?:boolean
} }
@ -27,10 +29,12 @@ export default class Room extends EventTarget
public name? : string; public name? : string;
public owner? : string; public owner? : string;
public peers : Map<string,Peer> = new Map(); public peers : Map<string,Peer> = new Map();
public info : RoomInfo;
constructor(wsts:MWSE){ constructor(wsts:MWSE){
super(); super();
this.mwse = wsts; this.mwse = wsts;
this.info = new RoomInfo(this);
} }
public setRoomOptions(options : IRoomOptions | string) public setRoomOptions(options : IRoomOptions | string)
{ {
@ -44,7 +48,8 @@ export default class Room extends EventTarget
accessType: "private", accessType: "private",
notifyActionInvite: true, notifyActionInvite: true,
notifyActionJoined: true, notifyActionJoined: true,
notifyActionEjected: true notifyActionEjected: true,
autoFetchInfo: true
}; };
Object.assign(defaultOptions,options); Object.assign(defaultOptions,options);
this.options = defaultOptions as IRoomOptions; this.options = defaultOptions as IRoomOptions;
@ -80,7 +85,8 @@ export default class Room extends EventTarget
let result = await this.mwse.EventPooling.request({ let result = await this.mwse.EventPooling.request({
type:'joinroom', type:'joinroom',
name: this.options.name, name: this.options.name,
credential: this.options.credential credential: this.options.credential,
autoFetchInfo: this.options.autoFetchInfo || false
}); });
if(result.status == 'fail') if(result.status == 'fail')
{ {
@ -90,6 +96,10 @@ export default class Room extends EventTarget
...this.options, ...this.options,
...result.room ...result.room
}; };
if(result.info)
{
this.info.info = result.info;
};
this.roomId = result.room.id; this.roomId = result.room.id;
this.mwse.rooms.set(this.roomId as string, this); this.mwse.rooms.set(this.roomId as string, this);
} }

48
frontend/RoomInfo.ts Normal file
View File

@ -0,0 +1,48 @@
import Room from "./Room";
export class RoomInfo
{
public room : Room;
public info : {[key:string]: any} = {};
constructor(room : Room){
this.room = room;
};
public async fetch(name?:string)
{
if(name)
{
let rinfo = await this.room.mwse.EventPooling.request(({
type: "room/getinfo",
roomId: this.room.roomId,
name
}));
if(rinfo.status == "success")
{
this.info = rinfo.info;
}else console.warn(rinfo.message);
}else{
let rinfo = await this.room.mwse.EventPooling.request(({
type: "peer/info",
peer: this.room.roomId
}));
if(rinfo.status == "success")
{
this.info = rinfo.info;
}else console.warn(rinfo.message);
};
return this.info;
}
public set(name: string, value: string | number)
{
this.info[name] = value;
this.room.mwse.WSTSProtocol.SendOnly({
type: "room/setinfo",
name,
value
});
}
public get(name?:string)
{
return name ? this.info[name] : this.info;
}
}