diff --git a/Source/Services/Room.js b/Source/Services/Room.js index 04cb4ae..6c2e556 100644 --- a/Source/Services/Room.js +++ b/Source/Services/Room.js @@ -67,6 +67,11 @@ function Room() */ this.waitingInvited = new Set(); + /** + * @type {Map} + */ + this.info = new Map(); + this.sync = function(...args){ process.nextTick(()=>{ for (const name of args) { @@ -117,6 +122,14 @@ Room.prototype.toJSON = function(detailed){ } return obj; }; +Room.prototype.getInfo = function(){ + let obj = {}; + for (const [name, value] of this.info) + { + obj[name] = value; + } + return obj; +}; /** * @param {Object} data * @param {Room} room @@ -244,7 +257,8 @@ let CreateRoomVerify = joi.object({ description: joi.string().required(), name: joi.string().required(), credential: joi.string().optional(), - ifexistsJoin: joi.boolean().optional() + ifexistsJoin: joi.boolean().optional(), + autoFetchInfo: joi.boolean().optional(), }); addService(({ @@ -412,6 +426,11 @@ addService(({ { if(room.credential == Sha256(message.credential + "")) { + let info = {}; + if(autoFetchInfo) + { + info.info = room.getInfo(); + }; room.join(client); return end({ status : "success", @@ -423,10 +442,16 @@ addService(({ area: "credential" }) }else if(room.joinType == "free"){ + let info = {}; + if(autoFetchInfo) + { + info.info = room.getInfo(); + }; room.join(client); return end({ status : "success", - room: room.toJSON() + room: room.toJSON(), + ...info }) }else if(room.joinType == "invite"){ room.waitingInvited.add(client.id); @@ -597,19 +622,94 @@ addService(({ } case 'room/list':{ let rooms = []; - for (const [id, {accessType,name,joinType,description}] of Room.rooms) if(accessType == "public"){ - rooms.push({ - name, - joinType, - description, - id - }) + for (const [id, {accessType,name,joinType,description}] of Room.rooms) + { + if(accessType == "public") + { + rooms.push({ + name, + joinType, + description, + id + }) + } } end({ type:'public/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:{ next(); } diff --git a/frontend/Room.ts b/frontend/Room.ts index bd71cf5..22aa548 100644 --- a/frontend/Room.ts +++ b/frontend/Room.ts @@ -1,6 +1,7 @@ import EventTarget from "./EventTarget"; import MWSE from "./index"; import Peer from "./Peer"; +import { RoomInfo } from "./RoomInfo"; export interface IRoomOptions { @@ -13,6 +14,7 @@ export interface IRoomOptions notifyActionInvite?: boolean; notifyActionJoined?: boolean; notifyActionEjected?: boolean; + autoFetchInfo?:boolean } @@ -27,10 +29,12 @@ export default class Room extends EventTarget public name? : string; public owner? : string; public peers : Map = new Map(); + public info : RoomInfo; constructor(wsts:MWSE){ super(); this.mwse = wsts; + this.info = new RoomInfo(this); } public setRoomOptions(options : IRoomOptions | string) { @@ -44,7 +48,8 @@ export default class Room extends EventTarget accessType: "private", notifyActionInvite: true, notifyActionJoined: true, - notifyActionEjected: true + notifyActionEjected: true, + autoFetchInfo: true }; Object.assign(defaultOptions,options); this.options = defaultOptions as IRoomOptions; @@ -80,7 +85,8 @@ export default class Room extends EventTarget let result = await this.mwse.EventPooling.request({ type:'joinroom', name: this.options.name, - credential: this.options.credential + credential: this.options.credential, + autoFetchInfo: this.options.autoFetchInfo || false }); if(result.status == 'fail') { @@ -90,6 +96,10 @@ export default class Room extends EventTarget ...this.options, ...result.room }; + if(result.info) + { + this.info.info = result.info; + }; this.roomId = result.room.id; this.mwse.rooms.set(this.roomId as string, this); } diff --git a/frontend/RoomInfo.ts b/frontend/RoomInfo.ts new file mode 100644 index 0000000..4cc6342 --- /dev/null +++ b/frontend/RoomInfo.ts @@ -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; + } +} \ No newline at end of file