Frontend Connection yapılandırması

This commit is contained in:
Abdussamed 2024-02-10 18:16:22 +03:00
parent ee6b6fc9c3
commit 5c30f871c7
14 changed files with 4922 additions and 411 deletions

View File

@ -57,13 +57,13 @@ Client.clients = new Map();
*/
Client.prototype.peerRequest = function(client){
let info = {};
this.store.forEach((value, name) => info[name] = value);
this.info.forEach((value, name) => info[name] = value);
this.pairs.add(client.id);
this.sync('pairs');
client.send([{
from: this.id,
info
},'request/pair']);
client.send([
{ from: this.id },
'request/pair'
]);
};
Client.prototype.match = function(filterObject){
@ -204,4 +204,17 @@ Client.prototype.send = function(obj){
}
};
Client.prototype.packWriteable = function(){
return !!this.store.get("packrecaive")
}
Client.prototype.packReadable = function(){
return !!this.store.get("packsending")
}
Client.prototype.peerInfoNotifiable = function(){
return !!this.store.get("notifyPairInfo")
}
Client.prototype.roomInfoNotifiable = function(){
return !!this.store.get("notifyRoomInfo")
}
exports.Client = Client;

View File

@ -14,6 +14,9 @@ process.on('message',data => {
break;
}
case "CLIENT_UPDATE_PROP":{
data.value = transformDeserialization(data.value, data.typing);
slog("CLIENT_UPDATE_PROP");
let client = Client.clients.get(data.uuid);
client[data.name] = data.value;
@ -40,6 +43,10 @@ process.on('message',data => {
break;
}
case "ROOM_UPDATE_PROP":{
data.value = transformDeserialization(data.value, data.typing);
slog("ROOM_UPDATE_PROP");
let room = Room.rooms.get(data.uuid);
room[data.name] = data.value;
@ -86,11 +93,16 @@ function CLIENT_CREATED(uuid)
function CLIENT_UPDATE_PROP(uuid, name, value)
{
mlog("CLIENT_UPDATE_PROP");
let typing = value.__proto__.constructor.name;
value = transformSerialization(value);
process.send({
type:'CLIENT_UPDATE_PROP',
uuid: uuid,
name,
value
value,
typing
})
};
function CLIENT_SEND_MESSAGE(uuid, message, clusterPid)
@ -126,11 +138,16 @@ function ROOM_CREATED(room)
function ROOM_UPDATE_PROP(uuid, name, value)
{
mlog("ROOM_UPDATE_PROP");
let typing = value.__proto__.constructor.name;
value = transformSerialization(value);
process.send({
type:'ROOM_UPDATE_PROP',
uuid: uuid,
name,
value
value,
typing
})
};
@ -173,6 +190,31 @@ function slog(command)
console.log("S",process.pid, command)
}
function transformSerialization(value)
{
switch(value.__proto__.constructor.name)
{
case "Map":{
return [...value];
}
case "Set":{
return [...value];
}
}
}
function transformDeserialization(value,type)
{
switch(type)
{
case "Map":{
return new Map(value);
}
case "Set":{
return new Set(value)
}
}
}
exports.CLIENT_CREATED = CLIENT_CREATED;
exports.CLIENT_UPDATE_PROP = CLIENT_UPDATE_PROP;

View File

@ -27,6 +27,13 @@ addService(({
{
case "pack/to":{
let {to,pack,handshake} = message;
if(!client.packReadable()){
handshake && end({
type: 'fail'
})
}
if(Client.clients.has(to))
{
let otherPeer = Client.clients.get(to);
@ -36,6 +43,12 @@ addService(({
type: 'fail'
})
}
if(!otherPeer.packWriteable()){
handshake && end({
type: 'fail'
})
}
otherPeer.send([{
from: client.id,
pack: pack
@ -85,6 +98,14 @@ addService(({
}
case "pack/room":{
let {to,pack, handshake,wom} = message;
if(!client.packReadable()){
handshake && end({
type: 'fail'
})
}
if(Room.rooms.has(to))
{
if(!client.rooms.has(to))
@ -93,11 +114,17 @@ addService(({
type: 'fail'
})
};
Room.rooms.get(to).send([{
from: to,
pack: pack,
sender: client.id
}, 'pack/room'], wom ? client.id : void 0);
Room.rooms.get(to).send(
[
{
from: to,
pack: pack,
sender: client.id
}, 'pack/room'
],
wom ? client.id : void 0,
client => client.packWriteable()
);
handshake && end({
type: 'success'
})

View File

@ -161,11 +161,19 @@ Room.fromJSON = function(data, room){
)
return room;
};
Room.prototype.send = function(obj, withOut){
/**
*
* @param {any} obj
* @param {string} withOut
* @param {(client:Client) => boolean} map
*/
Room.prototype.send = function(obj, withOut, map){
for (const client of this.clients.values()) {
if(client.id != withOut)
{
client.send(obj);
(
map ? map(client) : 1
) && client.send(obj);
}
}
termoutput && term.green("Room bulk message ").white(this.name," in ").yellow(this.clients.size + "").white(" clients")('\n');
@ -176,11 +184,18 @@ Room.prototype.send = function(obj, withOut){
Room.prototype.join = function(client){
if(this.notifyActionJoined)
{
this.send([{
id: client.id,
roomid: this.id,
ownerid: this.owner.id
},'room/joined']);
this.send(
[
{
id: client.id,
roomid: this.id,
ownerid: this.owner.id
},
'room/joined'
],
void 0,
client => client.peerInfoNotifiable()
);
};
client.rooms.add(this.id);
this.clients.set(client.id, client);
@ -203,11 +218,18 @@ Room.prototype.down = function(){
Room.prototype.eject = function(client){
if(this.notifyActionEjected)
{
this.send([{
id: client.id,
roomid: this.id,
ownerid: this.owner.id
},'room/ejected']);
this.send(
[
{
id: client.id,
roomid: this.id,
ownerid: this.owner.id
},
'room/ejected'
],
void 0,
client => client.peerInfoNotifiable()
);
}
client.rooms.delete(this.id);
this.clients.delete(client.id);
@ -697,14 +719,20 @@ addService(({
}
room.info.set(name, value);
room.send([
{
name,
value,
roomId:room.id
},
"room/info"
], client.id);
room.send(
[
{
name,
value,
roomId:room.id
},
"room/info"
],
client.id,
client => {
return client.roomInfoNotifiable()
}
);
return end({
status: "success"

View File

@ -0,0 +1,69 @@
let {addService,addListener} = require("../WebSocket.js");
const Stdoutput = [
["notifyPairInfo", false],
["packrecaive", true],
["packsending", true],
["notifyRoomInfo", true]
];
addListener('connect',(global, client)=>{
for (const [name, defaultValue] of Stdoutput)
{
client.store.set(name, defaultValue);
}
client.sync('store');
});
addService(({
client,
end,
global,
message,
next,
response
})=>{
let {type, value} = message;
switch(type)
{
// enable/disable pair/info messages
case 'connection/pairinfo':{
client.store.set("notifyPairInfo", !!value)
client.sync('store');
end(true);
break;
}
case 'connection/roominfo':{
client.store.set("notifyRoomInfo", !!value)
client.sync('store');
end(true);
break;
}
case 'connection/packrecaive':{
client.store.set("packrecaive", !!value)
client.sync('store');
end(true);
break;
}
case 'connection/packsending':{
client.store.set("packsending", !!value)
client.sync('store');
end(true);
break;
}
case 'connection/reset':{
for (const [name, defaultValue] of Stdoutput)
{
client.store.set(name, defaultValue);
}
client.sync('store');
end(true);
break;
}
}
});

View File

@ -6,6 +6,7 @@ require("./Services/Auth.js");
require("./Services/Room.js");
require("./Services/DataTransfer.js");
require("./Services/IPPressure.js");
require("./Services/Session.js");
process.on('unhandledRejection',()=>{

View File

@ -29,9 +29,6 @@ export default class Peer extends EventTarget
super();
this.mwse = wsts;
this.info = new PeerInfo(this);
this.on('updateinfo',(name:string,value:any) => {
this.info.info[name] = value;
})
this.on('pack',(data:{type?:string,action?:IMessageSymbase,payload?:any}) => {
if(data.type == ':rtcbase_pack:')
{
@ -121,6 +118,18 @@ export default class Peer extends EventTarget
value: 'disable'
});
}
async enablePairInfo(){
await this.mwse.EventPooling.request({
type: 'connection/pairinfo',
value: true
});
}
async disablePairInfo(){
await this.mwse.EventPooling.request({
type: 'connection/pairinfo',
value: false
});
}
async requestPair()
{
let {message,status} = await this.mwse.EventPooling.request({
@ -204,6 +213,9 @@ export default class Peer extends EventTarget
if(sendChannel == "websocket")
{
if(!this.mwse.writable){
return console.warn("Socket is not writable");
}
await this.mwse.EventPooling.request({
type:'pack/to',
pack,

View File

@ -116,6 +116,9 @@ export default class Room extends EventTarget
}
}
async send(pack: any, wom:boolean = false){
if(!this.mwse.writable){
return console.warn("Socket is not writable");
}
await this.mwse.EventPooling.request({
type:'pack/room',
pack,

View File

@ -6,6 +6,9 @@ export class RoomInfo
public info : {[key:string]: any} = {};
constructor(room : Room){
this.room = room;
this.room.on('updateinfo',(name:string,value:any) => {
this.info[name] = value;
})
};
public async fetch(name?:string)
{
@ -18,16 +21,16 @@ export class RoomInfo
}));
if(rinfo.status == "success")
{
this.info = rinfo.info;
this.info = rinfo.value;
}else console.warn(rinfo.message);
}else{
let rinfo = await this.room.mwse.EventPooling.request(({
type: "peer/info",
peer: this.room.roomId
type: "room/info",
roomId: this.room.roomId
}));
if(rinfo.status == "success")
{
this.info = rinfo.info;
this.info = rinfo.value;
}else console.warn(rinfo.message);
};
return this.info;

View File

@ -52,6 +52,35 @@ export default class MWSE extends EventTarget {
});
this.packMessagingSystem();
}
public writable = 1;
public readable = 1;
public enableRecaiveData(){
this.WSTSProtocol.SendOnly({ type: 'connection/packrecaive', value: 1 })
this.readable = 1
}
public disableRecaiveData(){
this.WSTSProtocol.SendOnly({ type: 'connection/packrecaive', value: 0 })
this.readable = 0
}
public enableSendData(){
this.WSTSProtocol.SendOnly({ type: 'connection/packsending', value: 1 })
this.writable = 1
}
public disableSendData(){
this.WSTSProtocol.SendOnly({ type: 'connection/packsending', value: 0 })
this.writable = 0
}
public enableNotifyRoomInfo(){
this.WSTSProtocol.SendOnly({ type: 'connection/roominfo', value: 1 })
}
public disableNotifyRoomInfo(){
this.WSTSProtocol.SendOnly({ type: 'connection/roominfo', value: 0 })
}
public async request(peerId: string, pack:Message)
{
let {pack:answer} = await this.EventPooling.request({
@ -73,8 +102,11 @@ export default class MWSE extends EventTarget {
private packMessagingSystem()
{
this.EventPooling.signal('pack',(payload : {from:string,pack:any}) => {
let {from,pack} = payload;
this.peer(from, true).emit('pack', pack);
if(this.readable)
{
let {from,pack} = payload;
this.peer(from, true).emit('pack', pack);
}
})
this.EventPooling.signal('request',(payload : {from:string,pack:any,id:number}) => {
let {from,pack, id} = payload;
@ -89,8 +121,11 @@ export default class MWSE extends EventTarget {
this.peer('me').emit('request', scope);
})
this.EventPooling.signal('pack/room',(payload : {from:string,pack:any,sender:string}) => {
let {from,pack,sender} = payload;
this.room(from).emit('message', pack, this.peer(sender));
if(this.readable)
{
let {from,pack,sender} = payload;
this.room(from).emit('message', pack, this.peer(sender));
}
})
this.EventPooling.signal('room/joined',(payload : {id:string,roomid:any,ownerid:string}) => {
let {id,roomid} = payload;

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -9,11 +9,11 @@
<body>
<h2><pre id="log"></pre></h2>
<h1 id="message"></h1>
<script src="http://ws.saqut.com/script"></script>
<script src="http://localhost:7707/script"></script>
<script>
async function main(){
const wsjs = new MWSE({
endpoint: "ws://ws.saqut.com"
endpoint: "ws://localhost:7707"
});
wsjs.scope(async ()=>{
let me = wsjs.peer('me');
@ -29,19 +29,28 @@
notifyActionEjected: false,
ifexistsJoin: true
});
room.on('updateinfo',(name,value) => {
console.log(`Odanın ${name} özelliği ${value} olarak değişti`)
})
window.room = room;
await room.createRoom();
room.on('message',(...args)=>{
gg.r++
gg()
console.log(args[0],gg.r)
});
iroom = room;
setInterval(()=>{
room.send({
type: "merhaba"
}, true)
})
gg.w++;
gg()
}, 200)
}, 2000)
});
};

752
yarn.lock

File diff suppressed because it is too large Load Diff