Room Messaging complete
This commit is contained in:
parent
ba05a83286
commit
d0712ad198
|
@ -51,7 +51,7 @@ addService(({
|
|||
break;
|
||||
}
|
||||
case "pack/room":{
|
||||
let {to,pack, handshake} = message;
|
||||
let {to,pack, handshake,wom} = message;
|
||||
if(Room.rooms.has(to))
|
||||
{
|
||||
if(!client.rooms.has(to))
|
||||
|
@ -63,7 +63,7 @@ addService(({
|
|||
Room.rooms.get(to).send([{
|
||||
from: client.id,
|
||||
pack: pack
|
||||
}, 'pack']);
|
||||
}, 'pack'], wom ? client.id : void 0);
|
||||
handshake && end({
|
||||
type: 'success'
|
||||
})
|
||||
|
|
|
@ -83,10 +83,13 @@ Room.prototype.toJSON = function(){
|
|||
obj.waitingInvited = [...this.waitingInvited];
|
||||
return obj;
|
||||
};
|
||||
Room.prototype.send = function(obj){
|
||||
Room.prototype.send = function(obj, withOut){
|
||||
for (const client of this.clients.values()) {
|
||||
if(client.id != withOut)
|
||||
{
|
||||
client.send(obj);
|
||||
}
|
||||
}
|
||||
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(),
|
||||
description: joi.string().required(),
|
||||
name: joi.string().required(),
|
||||
credential: joi.string().optional()
|
||||
credential: joi.string().optional(),
|
||||
ifexistsJoin: joi.boolean().optional()
|
||||
});
|
||||
|
||||
addService(({
|
||||
|
|
|
@ -4,9 +4,7 @@ import MWSE from "./index";
|
|||
interface IPeerOptions{
|
||||
|
||||
};
|
||||
interface IPeerMetadata{
|
||||
|
||||
};
|
||||
|
||||
export default class Peer extends EventTarget
|
||||
{
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import EventTarget from "./EventTarget";
|
||||
import MWSE from "./index";
|
||||
|
||||
export interface IRoomOptions
|
||||
|
@ -14,34 +15,79 @@ export interface IRoomOptions
|
|||
}
|
||||
|
||||
|
||||
export default class Room
|
||||
export default class Room extends EventTarget
|
||||
{
|
||||
public mwse : MWSE;
|
||||
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){
|
||||
super();
|
||||
this.mwse = wsts;
|
||||
}
|
||||
public setRoomOptions(options : IRoomOptions)
|
||||
public setRoomOptions(options : IRoomOptions | string)
|
||||
{
|
||||
if(typeof options == "string")
|
||||
{
|
||||
this.roomId = options;
|
||||
}else{
|
||||
this.options = options;
|
||||
}
|
||||
private emit(eventName :string, ...args:any[])
|
||||
{
|
||||
if(this.events[eventName])
|
||||
{
|
||||
for (const callback of this.events[eventName]) {
|
||||
callback(...args);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
public on(eventName :string, callback:Function)
|
||||
{
|
||||
if(this.events[eventName])
|
||||
{
|
||||
this.events[eventName].push(callback)
|
||||
throw new Error(result.message || result.messages);
|
||||
}else{
|
||||
this.events[eventName] = [callback];
|
||||
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
|
||||
});
|
||||
}
|
||||
}
|
|
@ -37,11 +37,28 @@ export default class MWSE extends EventTarget {
|
|||
let {to,pack} = payload;
|
||||
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);
|
||||
room.setRoomOptions(options);
|
||||
this.emit('room');
|
||||
return room;
|
||||
}
|
||||
public peer(options: string | IRoomOptions) : Peer
|
||||
|
|
|
@ -184,10 +184,19 @@ class MWSE extends (0, _eventTargetDefault.default) {
|
|||
let { to , pack } = payload;
|
||||
this.peer(to).emit("message", pack);
|
||||
});
|
||||
this.EventPooling.signal("pack/room", (payload)=>{
|
||||
let { to , pack } = payload;
|
||||
this.room(to).emit("message", pack);
|
||||
});
|
||||
}
|
||||
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);
|
||||
room.setRoomOptions(options);
|
||||
this.emit("room");
|
||||
return room;
|
||||
}
|
||||
peer(options) {
|
||||
|
@ -411,27 +420,64 @@ exports.default = EventTarget;
|
|||
},{"@parcel/transformer-js/src/esmodule-helpers.js":"i1YYE"}],"7qlv2":[function(require,module,exports) {
|
||||
var parcelHelpers = require("@parcel/transformer-js/src/esmodule-helpers.js");
|
||||
parcelHelpers.defineInteropFlag(exports);
|
||||
class Room {
|
||||
events = {};
|
||||
var _eventTarget = require("./EventTarget");
|
||||
var _eventTargetDefault = parcelHelpers.interopDefault(_eventTarget);
|
||||
class Room extends (0, _eventTargetDefault.default) {
|
||||
constructor(wsts){
|
||||
super();
|
||||
this.mwse = wsts;
|
||||
}
|
||||
setRoomOptions(options) {
|
||||
this.options = options;
|
||||
if (typeof options == "string") this.roomId = options;
|
||||
else this.options = options;
|
||||
}
|
||||
emit(eventName, ...args) {
|
||||
if (this.events[eventName]) for (const callback of this.events[eventName])callback(...args);
|
||||
setRoomId(uuid) {
|
||||
this.roomId = uuid;
|
||||
}
|
||||
on(eventName, callback) {
|
||||
if (this.events[eventName]) this.events[eventName].push(callback);
|
||||
else this.events[eventName] = [
|
||||
callback
|
||||
];
|
||||
async createRoom(roomOptions) {
|
||||
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, wom = false) {
|
||||
await this.mwse.EventPooling.request({
|
||||
type: "pack/room",
|
||||
pack,
|
||||
to: this.roomId,
|
||||
wom
|
||||
});
|
||||
}
|
||||
}
|
||||
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");
|
||||
parcelHelpers.defineInteropFlag(exports);
|
||||
class WSTSProtocol {
|
||||
|
|
File diff suppressed because one or more lines are too long
27
test.html
27
test.html
|
@ -9,18 +9,30 @@
|
|||
<body>
|
||||
<script src="./script/index.js"></script>
|
||||
<script>
|
||||
var wsjs;
|
||||
var wsjs, iroom;
|
||||
async function main(){
|
||||
wsjs = new MWSE({
|
||||
endpoint: "ws://localhost:7707"
|
||||
});
|
||||
wsjs.scope(()=>{
|
||||
wsjs.scope(async ()=>{
|
||||
let me = wsjs.peer('me');
|
||||
me.disablePairAuth();
|
||||
console.log(me.socketId);
|
||||
me.on('message',(...args)=>{
|
||||
console.log(args);
|
||||
})
|
||||
let room = wsjs.room({
|
||||
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)=>{
|
||||
peer.on('message',(...args)=>{
|
||||
|
@ -29,11 +41,6 @@
|
|||
})
|
||||
};
|
||||
main();
|
||||
function sendMessage(id)
|
||||
{
|
||||
let me = wsjs.peer(id);
|
||||
me.send("Merhaba");
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue