MWSE/frontend/Peer.ts

83 lines
2.0 KiB
TypeScript

import EventTarget from "./EventTarget";
import MWSE from "./index";
interface IPeerOptions{
};
export default class Peer extends EventTarget
{
public mwse : MWSE;
public options : IPeerOptions = {};
public socketId? : string;
public selfSocket : boolean = false;
public active : boolean = false;
constructor(wsts:MWSE){
super();
this.mwse = wsts;
}
setPeerOptions(options: string | IPeerOptions){
if(typeof options == "string")
{
this.setSocketId(options)
}else{
this.options = options;
}
}
setSocketId(uuid: string){
this.socketId = uuid;
}
async metadata() : Promise<any>
{
if(this.socketId == 'me')
{
let result = await this.mwse.EventPooling.request({
type:'my/socketid'
});
this.selfSocket = true;
this.active ||= true;
this.socketId = result;
this.emit('scope');
this.activeScope = true;
return result;
}
};
async request(pack:any){
if(this.active)
{
return await this.mwse.request(this.socketId as string, pack);
}
};
equalTo(peer : Peer | {socketId: string})
{
return this.socketId == peer.socketId;
}
async isReachable()
{
return await this.mwse.EventPooling.request({
type:'is/reachable',
to: this.socketId
});
}
async enablePairAuth(){
await this.mwse.EventPooling.request({
type:'auth/pair-system',
value: 'everybody'
});
}
async disablePairAuth(){
await this.mwse.EventPooling.request({
type:'auth/pair-system',
value: 'disable'
});
}
async send(pack: any){
await this.mwse.EventPooling.request({
type:'pack/to',
pack,
to: this.socketId
});
}
}