Handleshake

This commit is contained in:
Abdussamed ULUTAŞ 2022-11-26 23:24:58 +03:00
parent 8966601f14
commit 7e54c8b7c3
5 changed files with 254 additions and 33 deletions

View File

@ -15,12 +15,61 @@ function Client()
this.store = new Map();
this.rooms = new Set();
this.pairs = new Set();
this.requiredPair = false;
};
/**
* @type {Map<string, Client>}
*/
Client.clients = new Map();
/**
* @param {Client} client
*/
Client.prototype.peerRequest = function(client){
let info = {};
this.store.forEach((value, name) => info[name] = value);
this.pairs.add(client.id);
client.send([{
from: this.id,
info
},'request/pair']);
};
/**
* @param {Client} client
*/
Client.prototype.acceptPeerRequest = function(client){
this.pairs.add(client.id);
client.send([{
from: this.id
},'accepted/pair']);
};
/**
* @param {Client} client
*/
Client.prototype.rejectPeerRequest = function(client){
this.pairs.delete(client.id);
client.pairs.delete(this.id);
client.send([{
from: this.id
},'rejected/pair']);
};
/**
* @param {Client|string} client
* @returns {Boolean}
*/
Client.prototype.isPaired = function(client){
if(typeof client == "string")
{
return Client.clients.get(client)?.pairs.has(this.id) && this.pairs.has(client)
}
return client.pairs.has(this.id) && this.pairs.has(client.id);
};
Client.prototype.pairList = function(){
return [...this.pairs.values()].filter(e => this.isPaired(e));
};
Client.prototype.send = function(obj){
this.socket.sendUTF(JSON.stringify(obj));
};

View File

@ -1,4 +1,11 @@
let {addService} = require("../WebSocket.js");
const { Client } = require("../Client.js");
let {addService, addListener} = require("../WebSocket.js");
addListener('disconnect',(global, xClient)=>{
for (const pair of xClient.pairs) {
}
})
addService(({
client,
@ -6,41 +13,139 @@ addService(({
end,
next
})=>{
let {type,username,password} = message;
if(type === 'auth/check')
let {type,username,password,to} = message;
switch(type)
{
let auth = client.store.has('user');
return end({
value: auth
})
};
if(type === 'auth/login')
{
if(username == 'admin' && password == '123456Kc')
{
case 'auth/public':{
client.requiredPair = false;
return end({
status: 'success'
})
}else{
return end({
status: 'fail'
value: 'success',
mode: 'public'
})
}
};
if(type === 'auth/logout')
{
let auth = client.store.has('user');
if(auth)
{
client.store.delete('user');
case 'auth/private':{
client.requiredPair = true;
return end({
status: 'success'
})
}else{
return end({
status: 'fail'
value: 'success',
mode: 'private'
})
}
};
next();
case 'request/pair':{
if(Client.clients.has(to)){
return end({
status: 'fail',
message: 'CLIENT-NOT-FOUND'
})
};
let pairclient = Client.clients.get(to);
if(pairclient.pairs.has(client.id))
{
return end({
status: 'success',
message: 'ALREADY-PAIRED'
})
}
if(client.pairs.add(to))
{
return end({
status: 'fail',
message: 'ALREADY-REQUESTED'
})
}
client.peerRequest(pairclient);
break;
}
case 'accept/pair':{
if(Client.clients.has(to)){
return end({
status: 'fail',
message: 'CLIENT-NOT-FOUND'
})
};
let pairclient = Client.clients.get(to);
if(pairclient.pairs.has(client.id))
{
return end({
status: 'success',
message: 'ALREADY-PAIRED'
})
}
if(!client.pairs.has(to))
{
return end({
status: 'fail',
message: 'NOT-REQUESTED-PAIR'
})
}
client.acceptPeerRequest(pairclient);
break;
}
case 'reject/pair':{
if(Client.clients.has(to)){
return end({
status: 'fail',
message: 'CLIENT-NOT-FOUND'
})
};
let pairclient = Client.clients.get(to);
if(pairclient.pairs.has(client.id))
{
return end({
status: 'success',
message: 'ALREADY-PAIRED'
})
}
if(!client.pairs.has(to))
{
return end({
status: 'fail',
message: 'NOT-REQUESTED-PAIR'
})
}
client.rejectPeerRequest(pairclient);
break;
}
case 'pair/list':{
end({
type:'pair/list',
value: pairList
})
break;
}
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'
})
}
}
default:{
next();
}
}
});

View File

@ -29,7 +29,8 @@ addService(({
let {to,pack,handshake} = message;
if(Client.clients.has(to))
{
Client.clients.get(to).send([{
let otherPeer = Client.clients.get(to);
otherPeer.send([{
from: client.id,
pack: pack
}, 'pack']);

View File

@ -116,7 +116,14 @@ WSJS.prototype.sendRaw = function(obj){
{
this.ws.send(JSON.stringify(obj))
};
}
};
WSJS.prototype.authWith = async function(username, password){
await this.request({
type: 'auth/login',

View File

@ -9,6 +9,65 @@
<body>
<script src="./wsjs.js"></script>
<script>
let wsjs = new WSJS({
endpoint: "ws://localhost:8282"
});
let room = wsjs.room({
name: "MY-ROOM",
description: "Gizli Odam",
joinType: "password",
credential: "123456Kc",
ifexistsJoin: true
});
room.on('joinpeer',(id)=>{
});
room.on('invitepeer',(id)=>{
});
room.on('acceptedinvite',(id)=>{
});
room.on('rejectedinvite',(id)=>{
});
peer.on('message',(pack)=>{
})
room.info();
room.send({
message: "HI!"
});
room.closeRoom();
room.ejectRoom();
room.fetchPeerList().then(e => console.log("e id listesi"))
let peer = wsjs.peer('07781835-0e96-4fc3-b7e1-8e3c67f9c5b7');
peer.pairRequest();
peer.on('accept',()=>{
peer.send({
message: "HI!"
});
});
peer.on('reject',()=>{
});
peer.on('message',(data)=>{
});
peer.on('close',(data)=>{
});
peer.close();
let ws = new WSJS();
ws.connect('ws://localhost:8282');
ws.scope(async ()=>{