Handleshake

This commit is contained in:
Abdussamed ULUTAŞ 2022-11-26 22:26:45 +03:00
parent c7f0ea440f
commit 2bee72f37d
2 changed files with 19 additions and 14 deletions

View File

@ -26,30 +26,30 @@ addService(({
switch(type)
{
case "pack/to":{
let {to,pack} = message;
let {to,pack,handshake} = message;
if(Client.clients.has(to))
{
Client.clients.get(to).send([{
from: client.id,
pack: pack
}, 'pack']);
end({
handshake && end({
type: 'success'
})
}else{
end({
handshake && end({
type: 'fail'
})
}
break;
}
case "pack/room":{
let {to,pack} = message;
let {to,pack, handshake} = message;
if(Room.rooms.has(to))
{
if(!client.rooms.has(to))
{
return end({
return handshake && end({
type: 'fail'
})
};
@ -57,11 +57,11 @@ addService(({
from: client.id,
pack: pack
}, 'pack']);
end({
handshake && end({
type: 'success'
})
}else{
end({
handshake && end({
type: 'fail'
})
}

View File

@ -1,24 +1,27 @@
function WSJS()
{
this.isActive = false;
this.ws = null;
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(url){
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"));
}
@ -167,16 +170,18 @@ WSJS.prototype.getRoomPeers = async function(id){
});
};
WSJS.prototype.sendPackToPeer = async function(roomId, pack){
return await this.request({
return await this.sendOnly({
type: 'pack/to',
to: roomId,
pack
pack,
handshake: false
});
};
WSJS.prototype.sendPackToRoom = async function(roomId, pack){
return await this.request({
return await this.sendOnly({
type: 'pack/room',
to: roomId,
pack
pack,
handshake: false
});
};