diff --git a/Source/Services/Auth.js b/Source/Services/Auth.js index 2254b1b..e12e256 100644 --- a/Source/Services/Auth.js +++ b/Source/Services/Auth.js @@ -127,6 +127,21 @@ addService(({ }) break; } + case 'is/reachable':{ + if(Client.clients.has(to)) + { + let otherPeer = Client.clients.get(to); + if(otherPeer.requiredPair && !otherPeer.pairs.has(to)) + { + end(false); + }else{ + end(true); + } + }else{ + end(false); + } + break; + } case 'auth/check':{ let auth = client.store.has('user'); return end({ diff --git a/frontend/Peer.ts b/frontend/Peer.ts index 933c1ca..f4de61d 100644 --- a/frontend/Peer.ts +++ b/frontend/Peer.ts @@ -43,6 +43,13 @@ export default class Peer extends EventTarget return result; } } + 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', diff --git a/frontend/Room.ts b/frontend/Room.ts index fb7bfef..fba2356 100644 --- a/frontend/Room.ts +++ b/frontend/Room.ts @@ -1,5 +1,6 @@ import EventTarget from "./EventTarget"; import MWSE from "./index"; +import Peer from "./Peer"; export interface IRoomOptions { @@ -26,6 +27,7 @@ export default class Room extends EventTarget public joinType? : "free"|"invite"|"password"|"lock"; public name? : string; public owner? : string; + public peers? : Map; constructor(wsts:MWSE){ super(); @@ -37,6 +39,14 @@ export default class Room extends EventTarget { this.roomId = options; }else{ + Object.assign({ + joinType: "free", + ifexistsJoin: true, + accessType: "private", + notifyActionInvite: true, + notifyActionJoined: true, + notifyActionEjected: true + },options); this.options = options; } } @@ -90,4 +100,18 @@ export default class Room extends EventTarget wom }); } + async fetchAllPeers(){ + let {type, peers} = await this.mwse.EventPooling.request({ + type:'room-peers', + roomId: this.roomId + }) as {type:"success"|"fail", peers: string[]}; + if(type == 'fail') + { + throw new Error("Cant using peers on room") + }else if(type == 'success'){ + for (const peer of peers) { + this.mwse.peer(peer); + } + } + } } \ No newline at end of file diff --git a/frontend/index.ts b/frontend/index.ts index 28d8097..2c32f80 100644 --- a/frontend/index.ts +++ b/frontend/index.ts @@ -41,7 +41,24 @@ export default class MWSE extends EventTarget { let {to,pack} = payload; this.room(to).emit('message', pack); }) - + this.EventPooling.signal('room/joined',(payload : {id:string,roomid:any,ownerid:string}) => { + let {id,roomid} = payload; + let room = this.room(roomid); + let peer = this.peer(id); + room.emit('join', peer); + }) + this.EventPooling.signal('room/ejected',(payload : {id:string,roomid:any,ownerid:string}) => { + let {id,roomid} = payload; + let room = this.room(roomid); + let peer = this.peer(id); + room.emit('eject', peer); + }) + this.EventPooling.signal('room/closed',(payload : {roomid:any}) => { + let {roomid} = payload; + let room = this.room(roomid); + room.emit('close'); + this.rooms.delete(roomid); + }) } public room(options: IRoomOptions | string) : Room { @@ -51,13 +68,10 @@ export default class MWSE extends EventTarget { { 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.rooms.set(room.roomId as string, room); this.emit('room'); return room; } @@ -76,6 +90,7 @@ export default class MWSE extends EventTarget { } let peer = new Peer(this); peer.setPeerOptions(options); + this.peers.set(peer.socketId as string, peer); this.emit('peer', peer); return peer; } diff --git a/package.json b/package.json index e219209..6095ce2 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,8 @@ "version": "0.1.0", "description": "Mikro WebSocket Engine", "scripts": { - "compile": "parcel watch --no-hmr" + "compile": "parcel watch --no-hmr", + "build": "parcel build" }, "source": "./frontend/index.ts", "targets": { diff --git a/script/index.js b/script/index.js index 046e3eb..df3c438 100644 --- a/script/index.js +++ b/script/index.js @@ -1,542 +1,2 @@ -// modules are defined as an array -// [ module function, map of requires ] -// -// map of requires is short require name -> numeric require -// -// anything defined in a previous bundle is accessed via the -// orig method which is the require for previous bundles - -(function (modules, entry, mainEntry, parcelRequireName, globalName) { - /* eslint-disable no-undef */ - var globalObject = - typeof globalThis !== 'undefined' - ? globalThis - : typeof self !== 'undefined' - ? self - : typeof window !== 'undefined' - ? window - : typeof global !== 'undefined' - ? global - : {}; - /* eslint-enable no-undef */ - - // Save the require from previous bundle to this closure if any - var previousRequire = - typeof globalObject[parcelRequireName] === 'function' && - globalObject[parcelRequireName]; - - var cache = previousRequire.cache || {}; - // Do not use `require` to prevent Webpack from trying to bundle this call - var nodeRequire = - typeof module !== 'undefined' && - typeof module.require === 'function' && - module.require.bind(module); - - function newRequire(name, jumped) { - if (!cache[name]) { - if (!modules[name]) { - // if we cannot find the module within our internal map or - // cache jump to the current global require ie. the last bundle - // that was added to the page. - var currentRequire = - typeof globalObject[parcelRequireName] === 'function' && - globalObject[parcelRequireName]; - if (!jumped && currentRequire) { - return currentRequire(name, true); - } - - // If there are other bundles on this page the require from the - // previous one is saved to 'previousRequire'. Repeat this as - // many times as there are bundles until the module is found or - // we exhaust the require chain. - if (previousRequire) { - return previousRequire(name, true); - } - - // Try the node require function if it exists. - if (nodeRequire && typeof name === 'string') { - return nodeRequire(name); - } - - var err = new Error("Cannot find module '" + name + "'"); - err.code = 'MODULE_NOT_FOUND'; - throw err; - } - - localRequire.resolve = resolve; - localRequire.cache = {}; - - var module = (cache[name] = new newRequire.Module(name)); - - modules[name][0].call( - module.exports, - localRequire, - module, - module.exports, - this - ); - } - - return cache[name].exports; - - function localRequire(x) { - var res = localRequire.resolve(x); - return res === false ? {} : newRequire(res); - } - - function resolve(x) { - var id = modules[name][1][x]; - return id != null ? id : x; - } - } - - function Module(moduleName) { - this.id = moduleName; - this.bundle = newRequire; - this.exports = {}; - } - - newRequire.isParcelRequire = true; - newRequire.Module = Module; - newRequire.modules = modules; - newRequire.cache = cache; - newRequire.parent = previousRequire; - newRequire.register = function (id, exports) { - modules[id] = [ - function (require, module) { - module.exports = exports; - }, - {}, - ]; - }; - - Object.defineProperty(newRequire, 'root', { - get: function () { - return globalObject[parcelRequireName]; - }, - }); - - globalObject[parcelRequireName] = newRequire; - - for (var i = 0; i < entry.length; i++) { - newRequire(entry[i]); - } - - if (mainEntry) { - // Expose entry point to Node, AMD or browser globals - // Based on https://github.com/ForbesLindesay/umd/blob/master/template.js - var mainExports = newRequire(mainEntry); - - // CommonJS - if (typeof exports === 'object' && typeof module !== 'undefined') { - module.exports = mainExports; - - // RequireJS - } else if (typeof define === 'function' && define.amd) { - define(function () { - return mainExports; - }); - - //