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){ Client.prototype.peerRequest = function(client){
let info = {}; let info = {};
this.store.forEach((value, name) => info[name] = value); this.info.forEach((value, name) => info[name] = value);
this.pairs.add(client.id); this.pairs.add(client.id);
this.sync('pairs'); this.sync('pairs');
client.send([{ client.send([
from: this.id, { from: this.id },
info 'request/pair'
},'request/pair']); ]);
}; };
Client.prototype.match = function(filterObject){ 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; exports.Client = Client;

View File

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

View File

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

View File

@ -161,11 +161,19 @@ Room.fromJSON = function(data, room){
) )
return 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()) { for (const client of this.clients.values()) {
if(client.id != withOut) 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'); 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){ Room.prototype.join = function(client){
if(this.notifyActionJoined) if(this.notifyActionJoined)
{ {
this.send([{ this.send(
[
{
id: client.id, id: client.id,
roomid: this.id, roomid: this.id,
ownerid: this.owner.id ownerid: this.owner.id
},'room/joined']); },
'room/joined'
],
void 0,
client => client.peerInfoNotifiable()
);
}; };
client.rooms.add(this.id); client.rooms.add(this.id);
this.clients.set(client.id, client); this.clients.set(client.id, client);
@ -203,11 +218,18 @@ Room.prototype.down = function(){
Room.prototype.eject = function(client){ Room.prototype.eject = function(client){
if(this.notifyActionEjected) if(this.notifyActionEjected)
{ {
this.send([{ this.send(
[
{
id: client.id, id: client.id,
roomid: this.id, roomid: this.id,
ownerid: this.owner.id ownerid: this.owner.id
},'room/ejected']); },
'room/ejected'
],
void 0,
client => client.peerInfoNotifiable()
);
} }
client.rooms.delete(this.id); client.rooms.delete(this.id);
this.clients.delete(client.id); this.clients.delete(client.id);
@ -697,14 +719,20 @@ addService(({
} }
room.info.set(name, value); room.info.set(name, value);
room.send([ room.send(
[
{ {
name, name,
value, value,
roomId:room.id roomId:room.id
}, },
"room/info" "room/info"
], client.id); ],
client.id,
client => {
return client.roomInfoNotifiable()
}
);
return end({ return end({
status: "success" 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/Room.js");
require("./Services/DataTransfer.js"); require("./Services/DataTransfer.js");
require("./Services/IPPressure.js"); require("./Services/IPPressure.js");
require("./Services/Session.js");
process.on('unhandledRejection',()=>{ process.on('unhandledRejection',()=>{

View File

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

View File

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

View File

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

View File

@ -52,6 +52,35 @@ export default class MWSE extends EventTarget {
}); });
this.packMessagingSystem(); 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) public async request(peerId: string, pack:Message)
{ {
let {pack:answer} = await this.EventPooling.request({ let {pack:answer} = await this.EventPooling.request({
@ -73,8 +102,11 @@ export default class MWSE extends EventTarget {
private packMessagingSystem() private packMessagingSystem()
{ {
this.EventPooling.signal('pack',(payload : {from:string,pack:any}) => { this.EventPooling.signal('pack',(payload : {from:string,pack:any}) => {
if(this.readable)
{
let {from,pack} = payload; let {from,pack} = payload;
this.peer(from, true).emit('pack', pack); this.peer(from, true).emit('pack', pack);
}
}) })
this.EventPooling.signal('request',(payload : {from:string,pack:any,id:number}) => { this.EventPooling.signal('request',(payload : {from:string,pack:any,id:number}) => {
let {from,pack, id} = payload; let {from,pack, id} = payload;
@ -89,8 +121,11 @@ export default class MWSE extends EventTarget {
this.peer('me').emit('request', scope); this.peer('me').emit('request', scope);
}) })
this.EventPooling.signal('pack/room',(payload : {from:string,pack:any,sender:string}) => { this.EventPooling.signal('pack/room',(payload : {from:string,pack:any,sender:string}) => {
if(this.readable)
{
let {from,pack,sender} = payload; let {from,pack,sender} = payload;
this.room(from).emit('message', pack, this.peer(sender)); this.room(from).emit('message', pack, this.peer(sender));
}
}) })
this.EventPooling.signal('room/joined',(payload : {id:string,roomid:any,ownerid:string}) => { this.EventPooling.signal('room/joined',(payload : {id:string,roomid:any,ownerid:string}) => {
let {id,roomid} = payload; 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> <body>
<h2><pre id="log"></pre></h2> <h2><pre id="log"></pre></h2>
<h1 id="message"></h1> <h1 id="message"></h1>
<script src="http://ws.saqut.com/script"></script> <script src="http://localhost:7707/script"></script>
<script> <script>
async function main(){ async function main(){
const wsjs = new MWSE({ const wsjs = new MWSE({
endpoint: "ws://ws.saqut.com" endpoint: "ws://localhost:7707"
}); });
wsjs.scope(async ()=>{ wsjs.scope(async ()=>{
let me = wsjs.peer('me'); let me = wsjs.peer('me');
@ -29,19 +29,28 @@
notifyActionEjected: false, notifyActionEjected: false,
ifexistsJoin: true 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(); await room.createRoom();
room.on('message',(...args)=>{ room.on('message',(...args)=>{
gg.r++ gg.r++
gg() gg()
console.log(args[0],gg.r)
}); });
iroom = room; iroom = room;
setInterval(()=>{ setInterval(()=>{
room.send({ room.send({
type: "merhaba" type: "merhaba"
}, true) })
gg.w++; gg.w++;
gg() gg()
}, 200) }, 2000)
}); });
}; };

752
yarn.lock

File diff suppressed because it is too large Load Diff