90 lines
2.2 KiB
TypeScript
90 lines
2.2 KiB
TypeScript
import EventTarget from "./EventTarget";
|
|
import { PeerInfo } from "./PeerInfo";
|
|
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;
|
|
public info : PeerInfo;
|
|
constructor(wsts:MWSE){
|
|
super();
|
|
this.mwse = wsts;
|
|
this.info = new PeerInfo(this);
|
|
}
|
|
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
|
|
});
|
|
}
|
|
async forget(){
|
|
this.mwse.peers.delete(this.socketId as string);
|
|
this.mwse.pairs.delete(this.socketId as string);
|
|
}
|
|
}
|