Peer info added
This commit is contained in:
parent
fd414cc225
commit
5268620358
|
@ -15,7 +15,17 @@ function Client()
|
|||
*/
|
||||
this.created_at = null;
|
||||
|
||||
/**
|
||||
* @type {Map<string,any>}
|
||||
*/
|
||||
this.info = new Map();
|
||||
/**
|
||||
* @type {Map<string,any>}
|
||||
*/
|
||||
this.store = new Map();
|
||||
/**
|
||||
* @type {Set<string>}
|
||||
*/
|
||||
this.rooms = new Set();
|
||||
this.pairs = new Set();
|
||||
this.requiredPair = false;
|
||||
|
@ -54,6 +64,74 @@ Client.prototype.peerRequest = function(client){
|
|||
},'request/pair']);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {Client|string} client
|
||||
*/
|
||||
Client.prototype.isSecure = function(client)
|
||||
{
|
||||
const { Room } = require("./Services/Room");
|
||||
if(typeof client == "string")
|
||||
{
|
||||
if(Client.clients.has(client))
|
||||
{
|
||||
client = Client.clients.get(client);
|
||||
}else return false;
|
||||
}else if(!(client instanceof Client)){
|
||||
return false;
|
||||
};
|
||||
|
||||
// Eşleştirilmiş kullanıcı
|
||||
if(this.isPaired(client))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Aynı odada bulunan kullanıcı
|
||||
for (const id of this.rooms) {
|
||||
let room = Room.rooms.get(id);
|
||||
if(room)
|
||||
{
|
||||
if(room.clients.has(id))
|
||||
{
|
||||
return true
|
||||
}
|
||||
}
|
||||
};
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @returns {{pairs:Map<string, Client>,roompairs:Map<string, Client>}}
|
||||
*/
|
||||
Client.prototype.getSucureClients = function()
|
||||
{
|
||||
const { Room } = require("./Services/Room");
|
||||
let pairs = new Map();
|
||||
let roompairs = new Map();
|
||||
|
||||
for (const [id, client] of this.pairs)
|
||||
{
|
||||
map.set(id, client)
|
||||
}
|
||||
|
||||
// Aynı odada bulunan kullanıcı
|
||||
for (const id of this.rooms) {
|
||||
let room = Room.rooms.get(id);
|
||||
if(room)
|
||||
{
|
||||
for (const [id, client] of room.clients)
|
||||
{
|
||||
if(id == this.id) continue;
|
||||
roompairs.set(id, client)
|
||||
};
|
||||
}
|
||||
};
|
||||
return {
|
||||
pairs,
|
||||
roompairs
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Client} client
|
||||
*/
|
||||
|
|
|
@ -8,7 +8,8 @@ addService(({
|
|||
end,
|
||||
next
|
||||
})=>{
|
||||
let {type,username,password,to,value} = message;
|
||||
let {type,to,value,name} = message;
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case "auth/pair-system":{
|
||||
|
@ -148,40 +149,99 @@ addService(({
|
|||
}
|
||||
break;
|
||||
}
|
||||
case 'auth/check':{
|
||||
let auth = client.store.has('user');
|
||||
// case 'auth/check':{
|
||||
// let auth = client.store.has('user');
|
||||
// return end({
|
||||
// value: auth
|
||||
// })
|
||||
// }
|
||||
// case 'auth/login':{
|
||||
// if(username == '*' && password == '*')
|
||||
// {
|
||||
// return end({
|
||||
// status: 'success'
|
||||
// })
|
||||
// }else{
|
||||
// return end({
|
||||
// status: 'fail'
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
// case 'auth/logout':{
|
||||
// let auth = client.store.has('user');
|
||||
// if(auth)
|
||||
// {
|
||||
// client.store.delete('user');
|
||||
// return end({
|
||||
// status: 'success'
|
||||
// })
|
||||
// }else{
|
||||
// return end({
|
||||
// status: 'fail'
|
||||
// })
|
||||
// }
|
||||
// }
|
||||
case 'auth/info':{
|
||||
client.info.set(name, value);
|
||||
let clients = client.getSucureClients();
|
||||
for (const [,spair] of clients.pairs)
|
||||
{
|
||||
spair.send([{
|
||||
from: client.id,
|
||||
name,
|
||||
value
|
||||
},"pair/info"]);
|
||||
};
|
||||
for (const [,spair] of clients.roompairs)
|
||||
{
|
||||
spair.send([{
|
||||
from: client.id,
|
||||
name,
|
||||
value
|
||||
},"pair/info"]);
|
||||
};
|
||||
process.send({
|
||||
type: 'AUTH/INFO',
|
||||
uuid: client.id,
|
||||
name,
|
||||
value
|
||||
});
|
||||
return end({
|
||||
value: auth
|
||||
})
|
||||
status: 'success'
|
||||
});
|
||||
}
|
||||
case 'auth/login':{
|
||||
if(username == '*' && password == '*')
|
||||
case 'peer/info':{
|
||||
if(client.isSecure(message.peer))
|
||||
{
|
||||
let info = {};
|
||||
client.info.forEach((value, name) => info[name] = value);
|
||||
return end({
|
||||
status: 'success'
|
||||
})
|
||||
status: "success",
|
||||
info
|
||||
});
|
||||
}else{
|
||||
return end({
|
||||
status: 'fail'
|
||||
})
|
||||
}
|
||||
}
|
||||
case 'auth/logout':{
|
||||
let auth = client.store.has('user');
|
||||
if(auth)
|
||||
{
|
||||
client.store.delete('user');
|
||||
return end({
|
||||
status: 'success'
|
||||
})
|
||||
}else{
|
||||
return end({
|
||||
status: 'fail'
|
||||
})
|
||||
status: "fail",
|
||||
message: "unaccessible user"
|
||||
});
|
||||
}
|
||||
}
|
||||
default:{
|
||||
next();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
process.on('message',({type, uuid, value, name}) => {
|
||||
switch(type)
|
||||
{
|
||||
case "AUTH/INFO":{
|
||||
let client = Client.clients.get(uuid);
|
||||
if(client)
|
||||
{
|
||||
client.info.set(name, value);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
|
@ -382,7 +382,6 @@ process.on('message',({type, uuid, value}) => {
|
|||
switch(type)
|
||||
{
|
||||
case "AP_NUMBER/LOCK":{
|
||||
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||
let client = Client.clients.get(uuid);
|
||||
APNumber.busyNumbers.set(value, client);
|
||||
if(client)
|
||||
|
@ -392,7 +391,6 @@ process.on('message',({type, uuid, value}) => {
|
|||
break;
|
||||
}
|
||||
case "AP_NUMBER/RELEASE":{
|
||||
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||
APNumber.busyNumbers.delete(value);
|
||||
let client = Client.clients.get(uuid);
|
||||
if(client)
|
||||
|
@ -402,7 +400,6 @@ process.on('message',({type, uuid, value}) => {
|
|||
break;
|
||||
}
|
||||
case "AP_SHORTCODE/LOCK":{
|
||||
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||
let client = Client.clients.get(uuid);
|
||||
APShortCode.busyCodes.set(value, client);
|
||||
if(client)
|
||||
|
@ -412,7 +409,6 @@ process.on('message',({type, uuid, value}) => {
|
|||
break;
|
||||
}
|
||||
case "AP_SHORTCODE/RELEASE":{
|
||||
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||
APShortCode.busyCodes.delete(value);
|
||||
let client = Client.clients.get(uuid);
|
||||
if(client)
|
||||
|
@ -422,7 +418,6 @@ process.on('message',({type, uuid, value}) => {
|
|||
break;
|
||||
}
|
||||
case "AP_IPADDRESS/LOCK":{
|
||||
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||
let client = Client.clients.get(uuid);
|
||||
APIPAddress.busyIP.set(value, client);
|
||||
if(client)
|
||||
|
@ -432,7 +427,6 @@ process.on('message',({type, uuid, value}) => {
|
|||
break;
|
||||
}
|
||||
case "AP_IPADDRESS/RELEASE":{
|
||||
console.log("S",process.pid, 'IPPressure SYNCED')
|
||||
APIPAddress.busyIP.delete(value);
|
||||
let client = Client.clients.get(uuid);
|
||||
if(client)
|
||||
|
|
|
@ -42,8 +42,6 @@ wsServer.addListener("connect",(socket) => {
|
|||
Client.clients.set(id, xClient);
|
||||
clients.set(id, xClient);
|
||||
|
||||
console.log("Client:", id,"on worker pid:",process.pid)
|
||||
|
||||
CLIENT_CREATED(id);
|
||||
|
||||
emit("connect", global, xClient);
|
||||
|
@ -80,7 +78,8 @@ wsServer.addListener("connect",(socket) => {
|
|||
if(typeof id === "string")
|
||||
{
|
||||
action = id;
|
||||
}
|
||||
id = void 0;
|
||||
};
|
||||
emitService(global, xClient, id, payload, action);
|
||||
}catch{
|
||||
emit("messageError", global, xClient, utf8Data);
|
||||
|
@ -145,10 +144,10 @@ async function emitService(global, client, id, payload, action)
|
|||
global,
|
||||
messageId: id,
|
||||
response:(obj)=>{
|
||||
client.send([obj, id, 'C']) // continue ([C]ONTINUE flag)
|
||||
id != undefined && client.send([obj, id, 'C']) // continue ([C]ONTINUE flag)
|
||||
},
|
||||
end:(obj)=>{
|
||||
client.send([obj, id, 'E']) // stopped data stream (this channel) ([E]ND flag)
|
||||
id != undefined && client.send([obj, id, 'E']) // stopped data stream (this channel) ([E]ND flag)
|
||||
},
|
||||
next:function(){
|
||||
willContinue = true;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import EventTarget from "./EventTarget";
|
||||
import { PeerInfo } from "./PeerInfo";
|
||||
import MWSE from "./index";
|
||||
|
||||
interface IPeerOptions{
|
||||
|
@ -13,9 +14,11 @@ export default class Peer extends EventTarget
|
|||
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")
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
import Peer from "./Peer";
|
||||
|
||||
export class PeerInfo
|
||||
{
|
||||
public peer : Peer;
|
||||
public info : {[key:string]: any} = {};
|
||||
constructor(mwse : Peer){
|
||||
this.peer = mwse;
|
||||
};
|
||||
public async fetch(name?:string)
|
||||
{
|
||||
if(name)
|
||||
{
|
||||
let rinfo = await this.peer.mwse.EventPooling.request(({
|
||||
type: "peer/info",
|
||||
peer: this.peer.socketId,
|
||||
name
|
||||
}));
|
||||
if(rinfo.status == "success")
|
||||
{
|
||||
this.info = rinfo.info;
|
||||
}else console.warn(rinfo.message);
|
||||
}else{
|
||||
let rinfo = await this.peer.mwse.EventPooling.request(({
|
||||
type: "peer/info",
|
||||
peer: this.peer.socketId
|
||||
}));
|
||||
if(rinfo.status == "success")
|
||||
{
|
||||
this.info = rinfo.info;
|
||||
}else console.warn(rinfo.message);
|
||||
};
|
||||
return this.info;
|
||||
}
|
||||
public set(name: string, value: string | number)
|
||||
{
|
||||
this.info[name] = value;
|
||||
this.peer.mwse.WSTSProtocol.SendOnly({
|
||||
type: "auth/info",
|
||||
name,
|
||||
value
|
||||
});
|
||||
}
|
||||
public get(name?:string)
|
||||
{
|
||||
return name ? this.info[name] : this.info;
|
||||
}
|
||||
}
|
|
@ -113,7 +113,7 @@ export default class Room extends EventTarget
|
|||
wom
|
||||
});
|
||||
}
|
||||
async fetchAllPeers(){
|
||||
async fetchPeers(){
|
||||
let {status, peers} = await this.mwse.EventPooling.request({
|
||||
type:'room-peers',
|
||||
roomId: this.roomId
|
||||
|
|
|
@ -95,6 +95,15 @@ export default class MWSE extends EventTarget {
|
|||
room.emit('close');
|
||||
this.rooms.delete(roomid);
|
||||
})
|
||||
this.EventPooling.signal("pair/info", (payload : {from : string,name: string, value: string | number | boolean}) => {
|
||||
let {from, name, value} = payload;
|
||||
|
||||
let peer = this.peer(from);
|
||||
|
||||
peer.info.info[name] = value;
|
||||
|
||||
peer.emit("info", name, value);
|
||||
})
|
||||
}
|
||||
public room(options: IRoomOptions | string) : Room
|
||||
{
|
||||
|
|
|
@ -1,194 +0,0 @@
|
|||
function WSJS()
|
||||
{
|
||||
this.isActive = false;
|
||||
this.ws = WSJS.activeWS || null;
|
||||
this.endpoint = null;
|
||||
};
|
||||
WSJS.activeWS = null;
|
||||
WSJS.prototype.connect = function(url){
|
||||
this.ws = new WebSocket(url);
|
||||
this.addListeners();
|
||||
}
|
||||
WSJS.prototype.addListeners = function(){
|
||||
this.ws.addEventListener("close", this.closedEvent.bind(this));
|
||||
this.ws.addEventListener("message", this.messageEvent.bind(this));
|
||||
this.ws.addEventListener("open", this.openMessage.bind(this));
|
||||
}
|
||||
WSJS.prototype.closedEvent = function(){
|
||||
WSJS.activeWS = null;
|
||||
this.isActive = false;
|
||||
this.ws = null;
|
||||
this.events.dispatchEvent(new Event("close"));
|
||||
}
|
||||
WSJS.prototype.openMessage = function(){
|
||||
WSJS.activeWS = this.ws;
|
||||
this.isActive = true;
|
||||
this.events.dispatchEvent(new Event("open"));
|
||||
}
|
||||
WSJS.prototype.messageEvent = function({data}){
|
||||
let [payload, id, action] = JSON.parse(data);
|
||||
if(typeof id === 'number')
|
||||
{
|
||||
if(this.requests.has(id))
|
||||
{
|
||||
this.requests.get(id)(payload, action);
|
||||
switch(action)
|
||||
{
|
||||
case 'E':{ // [E]ND flag
|
||||
this.requests.delete(id);
|
||||
break;
|
||||
}
|
||||
case 'S': // [S]TREAM flag
|
||||
default:{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else console.warn("Missing event sended from server");
|
||||
}else{
|
||||
if(this.signals.has(id))
|
||||
{
|
||||
for (const callback of this.signals.get(id)) {
|
||||
callback(payload);
|
||||
}
|
||||
}else console.warn("Missing event sended from server");
|
||||
}
|
||||
}
|
||||
WSJS.prototype.events = new EventTarget();
|
||||
WSJS.prototype.scope = function(func){
|
||||
if(this.isActive)
|
||||
{
|
||||
func();
|
||||
}else this.events.addEventListener("open", func);
|
||||
}
|
||||
|
||||
WSJS.prototype.sendOnly = function(obj){
|
||||
if(this.isActive)
|
||||
{
|
||||
this.sendRaw([obj,'R']);
|
||||
}else throw new Error(`socket could be a active`);
|
||||
}
|
||||
WSJS.prototype.requests = new Map();
|
||||
WSJS.prototype.requestCount = 0;
|
||||
WSJS.prototype.request = async function(obj){
|
||||
if(this.isActive)
|
||||
{
|
||||
return await new Promise(ok => {
|
||||
let id = ++this.requestCount;
|
||||
this.sendRaw([obj,id,'R']);
|
||||
this.requests.set(id,data => {
|
||||
ok(data);
|
||||
});
|
||||
})
|
||||
}else throw new Error(`socket could be a active`);
|
||||
}
|
||||
WSJS.prototype.stream = async function(obj,callback){
|
||||
if(this.isActive)
|
||||
{
|
||||
let id = ++this.requestCount;
|
||||
this.sendRaw([obj, id, 'S']);
|
||||
this.requests.set(id,data => {
|
||||
callback(data);
|
||||
});
|
||||
}else throw new Error(`socket could be a active`);
|
||||
}
|
||||
WSJS.prototype.signals = new Map();
|
||||
WSJS.prototype.signal = async function(name, callback){
|
||||
if(!this.signals.has(name))
|
||||
{
|
||||
this.signals.set(name, [callback]);
|
||||
}else{
|
||||
this.signals.get(name).push(callback);
|
||||
}
|
||||
}
|
||||
WSJS.prototype.slot = async function(name, obj){
|
||||
if(this.isActive)
|
||||
{
|
||||
if(typeof name == "string")
|
||||
{
|
||||
this.sendOnly([obj,name]);
|
||||
}else{
|
||||
throw new Error(`name could be a string, gived ${typeof name}`);
|
||||
}
|
||||
}else throw new Error(`socket could be a active`);
|
||||
}
|
||||
WSJS.prototype.sendRaw = function(obj){
|
||||
if(this.isActive)
|
||||
{
|
||||
this.ws.send(JSON.stringify(obj))
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
WSJS.prototype.authWith = async function(username, password){
|
||||
await this.request({
|
||||
type: 'auth/login',
|
||||
username,
|
||||
password
|
||||
});
|
||||
};
|
||||
WSJS.prototype.fetchMyRoomInfo = async function(){
|
||||
return await this.request({
|
||||
type: 'myroom-info'
|
||||
});
|
||||
};
|
||||
WSJS.prototype.createRoom = async function(options){
|
||||
let result = await this.request({
|
||||
type: 'create-room',
|
||||
accessType: options.accessType || "private",
|
||||
notifyActionInvite: options.notifyActionInvite === undefined ? true : options.notifyActionInvite,
|
||||
notifyActionJoined: options.notifyActionJoined === undefined ? true : options.notifyActionJoined,
|
||||
notifyActionEjected: options.notifyActionEjected === undefined ? true : options.notifyActionEjected,
|
||||
joinType: options.joinType || "free",
|
||||
description: options.description || "No Description",
|
||||
name: options.name || "No",
|
||||
credential: options.credential || undefined
|
||||
});
|
||||
return result;
|
||||
};
|
||||
WSJS.prototype.roomInfo = async function(name){
|
||||
let result = await this.request({
|
||||
type: 'room-info',
|
||||
name
|
||||
});
|
||||
return result;
|
||||
};
|
||||
WSJS.prototype.joinRoom = async function(options){
|
||||
let result = await this.request({
|
||||
...options,
|
||||
type: 'joinroom'
|
||||
});
|
||||
return result;
|
||||
};
|
||||
WSJS.prototype.joinedRooms = new Map();
|
||||
WSJS.prototype.getJoinedRooms = async function(){
|
||||
return await this.request({
|
||||
type: 'joinedrooms'
|
||||
});
|
||||
};
|
||||
WSJS.prototype.getRoomPeers = async function(id){
|
||||
return await this.request({
|
||||
type: 'room-peers',
|
||||
roomId: id
|
||||
});
|
||||
};
|
||||
WSJS.prototype.sendPackToPeer = async function(roomId, pack){
|
||||
return await this.sendOnly({
|
||||
type: 'pack/to',
|
||||
to: roomId,
|
||||
pack,
|
||||
handshake: false
|
||||
});
|
||||
};
|
||||
WSJS.prototype.sendPackToRoom = async function(roomId, pack){
|
||||
return await this.sendOnly({
|
||||
type: 'pack/room',
|
||||
to: roomId,
|
||||
pack,
|
||||
handshake: false
|
||||
});
|
||||
};
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue