Room Messaging complete

This commit is contained in:
Abdussamed ULUTAŞ 2022-12-16 23:54:10 +03:00
parent ba05a83286
commit d0712ad198
8 changed files with 167 additions and 49 deletions

View File

@ -51,7 +51,7 @@ addService(({
break; break;
} }
case "pack/room":{ case "pack/room":{
let {to,pack, handshake} = message; let {to,pack, handshake,wom} = message;
if(Room.rooms.has(to)) if(Room.rooms.has(to))
{ {
if(!client.rooms.has(to)) if(!client.rooms.has(to))
@ -63,7 +63,7 @@ addService(({
Room.rooms.get(to).send([{ Room.rooms.get(to).send([{
from: client.id, from: client.id,
pack: pack pack: pack
}, 'pack']); }, 'pack'], wom ? client.id : void 0);
handshake && end({ handshake && end({
type: 'success' type: 'success'
}) })

View File

@ -83,9 +83,12 @@ Room.prototype.toJSON = function(){
obj.waitingInvited = [...this.waitingInvited]; obj.waitingInvited = [...this.waitingInvited];
return obj; return obj;
}; };
Room.prototype.send = function(obj){ Room.prototype.send = function(obj, withOut){
for (const client of this.clients.values()) { for (const client of this.clients.values()) {
client.send(obj); if(client.id != withOut)
{
client.send(obj);
}
} }
term.green("Room bulk message ").white(this.name," in ").yellow(this.clients.size + "").white(" clients")('\n'); term.green("Room bulk message ").white(this.name," in ").yellow(this.clients.size + "").white(" clients")('\n');
}; };
@ -171,7 +174,8 @@ let CreateRoomVerify = joi.object({
joinType: joi.string().pattern(/^free$|^invite$|^password$|^lock$/).required(), joinType: joi.string().pattern(/^free$|^invite$|^password$|^lock$/).required(),
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()
}); });
addService(({ addService(({

View File

@ -4,9 +4,7 @@ import MWSE from "./index";
interface IPeerOptions{ interface IPeerOptions{
}; };
interface IPeerMetadata{
};
export default class Peer extends EventTarget export default class Peer extends EventTarget
{ {

View File

@ -1,3 +1,4 @@
import EventTarget from "./EventTarget";
import MWSE from "./index"; import MWSE from "./index";
export interface IRoomOptions export interface IRoomOptions
@ -14,34 +15,79 @@ export interface IRoomOptions
} }
export default class Room export default class Room extends EventTarget
{ {
public mwse : MWSE; public mwse : MWSE;
public options! : IRoomOptions; public options! : IRoomOptions;
public events : {[key:string]:Function[]} = {}; public roomId? : string;
public accessType? : "public"|"private";
public description? : string;
public joinType? : "free"|"invite"|"password"|"lock";
public name? : string;
public owner? : string;
constructor(wsts:MWSE){ constructor(wsts:MWSE){
super();
this.mwse = wsts; this.mwse = wsts;
} }
public setRoomOptions(options : IRoomOptions) public setRoomOptions(options : IRoomOptions | string)
{ {
this.options = options; if(typeof options == "string")
}
private emit(eventName :string, ...args:any[])
{
if(this.events[eventName])
{ {
for (const callback of this.events[eventName]) { this.roomId = options;
callback(...args);
}
}
}
public on(eventName :string, callback:Function)
{
if(this.events[eventName])
{
this.events[eventName].push(callback)
}else{ }else{
this.events[eventName] = [callback]; this.options = options;
} }
} }
setRoomId(uuid: string){
this.roomId = uuid;
}
async createRoom(roomOptions : IRoomOptions){
let options = this.options || roomOptions;
let result = await this.mwse.EventPooling.request({
type:'create-room',
...options
});
if(result.status == 'fail')
{
if(result.message == "ALREADY-EXISTS")
{
return this.join();
}
throw new Error(result.message || result.messages);
}else{
this.options = {
...this.options,
...result.room
};
this.roomId = result.room.id;
}
}
async join(){
let result = await this.mwse.EventPooling.request({
type:'joinroom',
name: this.options.name,
credential: this.options.credential
});
if(result.status == 'fail')
{
throw new Error(result.message);
}else{
this.options = {
...this.options,
...result.room
};
this.roomId = result.room.id;
}
}
async send(pack: any, wom:boolean = false){
await this.mwse.EventPooling.request({
type:'pack/room',
pack,
to: this.roomId,
wom
});
}
} }

View File

@ -37,11 +37,28 @@ export default class MWSE extends EventTarget {
let {to,pack} = payload; let {to,pack} = payload;
this.peer(to).emit('message', pack); this.peer(to).emit('message', pack);
}) })
this.EventPooling.signal('pack/room',(payload : {to:string,pack:any}) => {
let {to,pack} = payload;
this.room(to).emit('message', pack);
})
} }
public room(options: IRoomOptions) public room(options: IRoomOptions | string) : Room
{ {
if(typeof options == "string")
{
if(this.rooms.has(options))
{
return this.rooms.get(options) as Room
}
if(this.rooms.has(options))
{
return this.rooms.get(options) as Room
}
}
let room = new Room(this); let room = new Room(this);
room.setRoomOptions(options); room.setRoomOptions(options);
this.emit('room');
return room; return room;
} }
public peer(options: string | IRoomOptions) : Peer public peer(options: string | IRoomOptions) : Peer

View File

@ -184,10 +184,19 @@ class MWSE extends (0, _eventTargetDefault.default) {
let { to , pack } = payload; let { to , pack } = payload;
this.peer(to).emit("message", pack); this.peer(to).emit("message", pack);
}); });
this.EventPooling.signal("pack/room", (payload)=>{
let { to , pack } = payload;
this.room(to).emit("message", pack);
});
} }
room(options) { room(options) {
if (typeof options == "string") {
if (this.rooms.has(options)) return this.rooms.get(options);
if (this.rooms.has(options)) return this.rooms.get(options);
}
let room = new (0, _roomDefault.default)(this); let room = new (0, _roomDefault.default)(this);
room.setRoomOptions(options); room.setRoomOptions(options);
this.emit("room");
return room; return room;
} }
peer(options) { peer(options) {
@ -411,27 +420,64 @@ exports.default = EventTarget;
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"7qlv2":[function(require,module,exports) { },{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"7qlv2":[function(require,module,exports) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js"); var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports); parcelHelpers.defineInteropFlag(exports);
class Room { var _eventTarget = require("./EventTarget");
events = {}; var _eventTargetDefault = parcelHelpers.interopDefault(_eventTarget);
class Room extends (0, _eventTargetDefault.default) {
constructor(wsts){ constructor(wsts){
super();
this.mwse = wsts; this.mwse = wsts;
} }
setRoomOptions(options) { setRoomOptions(options) {
this.options = options; if (typeof options == "string") this.roomId = options;
else this.options = options;
} }
emit(eventName, ...args) { setRoomId(uuid) {
if (this.events[eventName]) for (const callback of this.events[eventName])callback(...args); this.roomId = uuid;
} }
on(eventName, callback) { async createRoom(roomOptions) {
if (this.events[eventName]) this.events[eventName].push(callback); let options = this.options || roomOptions;
else this.events[eventName] = [ let result = await this.mwse.EventPooling.request({
callback type: "create-room",
]; ...options
});
if (result.status == "fail") {
if (result.message == "ALREADY-EXISTS") return this.join();
throw new Error(result.message || result.messages);
} else {
this.options = {
...this.options,
...result.room
};
this.roomId = result.room.id;
}
}
async join() {
let result = await this.mwse.EventPooling.request({
type: "joinroom",
name: this.options.name,
credential: this.options.credential
});
if (result.status == "fail") throw new Error(result.message);
else {
this.options = {
...this.options,
...result.room
};
this.roomId = result.room.id;
}
}
async send(pack, wom = false) {
await this.mwse.EventPooling.request({
type: "pack/room",
pack,
to: this.roomId,
wom
});
} }
} }
exports.default = Room; exports.default = Room;
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"3kvWC":[function(require,module,exports) { },{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE","./EventTarget":"lleyn"}],"3kvWC":[function(require,module,exports) {
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js"); var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
parcelHelpers.defineInteropFlag(exports); parcelHelpers.defineInteropFlag(exports);
class WSTSProtocol { class WSTSProtocol {

File diff suppressed because one or more lines are too long

View File

@ -9,18 +9,30 @@
<body> <body>
<script src="./script/index.js"></script> <script src="./script/index.js"></script>
<script> <script>
var wsjs; var wsjs, iroom;
async function main(){ async function main(){
wsjs = new MWSE({ wsjs = new MWSE({
endpoint: "ws://localhost:7707" endpoint: "ws://localhost:7707"
}); });
wsjs.scope(()=>{ wsjs.scope(async ()=>{
let me = wsjs.peer('me'); let me = wsjs.peer('me');
me.disablePairAuth(); me.disablePairAuth();
console.log(me.socketId); console.log(me.socketId);
me.on('message',(...args)=>{ let room = wsjs.room({
console.log(args); accessType: "public",
}) description: "Benim odam",
joinType: "free",
name: "M.E.",
notifyActionInvite: true,
notifyActionJoined: true,
notifyActionEjected: false,
ifexistsJoin: true
});
await room.createRoom();
room.on('message',(...args)=>{
console.log(args)
});
iroom = room;
}); });
wsjs.on('peer',(peer)=>{ wsjs.on('peer',(peer)=>{
peer.on('message',(...args)=>{ peer.on('message',(...args)=>{
@ -29,11 +41,6 @@
}) })
}; };
main(); main();
function sendMessage(id)
{
let me = wsjs.peer(id);
me.send("Merhaba");
}
</script> </script>
</body> </body>
</html> </html>