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

View File

@ -1,24 +1,27 @@
function WSJS() function WSJS()
{ {
this.isActive = false; this.isActive = false;
this.ws = null; this.ws = WSJS.activeWS || null;
this.endpoint = null; this.endpoint = null;
}; };
WSJS.activeWS = null;
WSJS.prototype.connect = function(url){ WSJS.prototype.connect = function(url){
this.ws = new WebSocket(url); this.ws = new WebSocket(url);
this.addListeners(); this.addListeners();
} }
WSJS.prototype.addListeners = function(url){ WSJS.prototype.addListeners = function(){
this.ws.addEventListener("close", this.closedEvent.bind(this)); this.ws.addEventListener("close", this.closedEvent.bind(this));
this.ws.addEventListener("message", this.messageEvent.bind(this)); this.ws.addEventListener("message", this.messageEvent.bind(this));
this.ws.addEventListener("open", this.openMessage.bind(this)); this.ws.addEventListener("open", this.openMessage.bind(this));
} }
WSJS.prototype.closedEvent = function(){ WSJS.prototype.closedEvent = function(){
WSJS.activeWS = null;
this.isActive = false; this.isActive = false;
this.ws = null; this.ws = null;
this.events.dispatchEvent(new Event("close")); this.events.dispatchEvent(new Event("close"));
} }
WSJS.prototype.openMessage = function(){ WSJS.prototype.openMessage = function(){
WSJS.activeWS = this.ws;
this.isActive = true; this.isActive = true;
this.events.dispatchEvent(new Event("open")); this.events.dispatchEvent(new Event("open"));
} }
@ -141,7 +144,7 @@ WSJS.prototype.createRoom = async function(options){
return result; return result;
}; };
WSJS.prototype.roomInfo = async function(name){ WSJS.prototype.roomInfo = async function(name){
let result = await this.request({ let result = await this.request({
type: 'room-info', type: 'room-info',
name name
}); });
@ -167,16 +170,18 @@ WSJS.prototype.getRoomPeers = async function(id){
}); });
}; };
WSJS.prototype.sendPackToPeer = async function(roomId, pack){ WSJS.prototype.sendPackToPeer = async function(roomId, pack){
return await this.request({ return await this.sendOnly({
type: 'pack/to', type: 'pack/to',
to: roomId, to: roomId,
pack pack,
handshake: false
}); });
}; };
WSJS.prototype.sendPackToRoom = async function(roomId, pack){ WSJS.prototype.sendPackToRoom = async function(roomId, pack){
return await this.request({ return await this.sendOnly({
type: 'pack/room', type: 'pack/room',
to: roomId, to: roomId,
pack pack,
handshake: false
}); });
}; };