133 lines
3.6 KiB
JavaScript
133 lines
3.6 KiB
JavaScript
function WSJS()
|
|
{
|
|
this.isActive = false;
|
|
this.ws = null;
|
|
this.endpoint = null;
|
|
};
|
|
WSJS.prototype.connect = function(url){
|
|
this.ws = new WebSocket(url);
|
|
this.addListeners();
|
|
}
|
|
WSJS.prototype.addListeners = function(url){
|
|
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(){
|
|
this.isActive = false;
|
|
this.ws = null;
|
|
this.events.dispatchEvent(new Event("close"));
|
|
}
|
|
WSJS.prototype.openMessage = function(){
|
|
this.isActive = true;
|
|
this.events.dispatchEvent(new Event("open"));
|
|
}
|
|
WSJS.prototype.messageEvent = function({data}){
|
|
let [payload, id, action] = JSON.parse(data);
|
|
if(typeof id === 'number')
|
|
{
|
|
if(this.requests.has(id))
|
|
{
|
|
this.requests.get(id)(payload, action);
|
|
switch(action)
|
|
{
|
|
case 'E':{
|
|
this.requests.delete(id);
|
|
break;
|
|
}
|
|
case 'S':
|
|
default:{
|
|
break;
|
|
}
|
|
}
|
|
}else console.warn("Missing event sended from server");
|
|
}else{
|
|
if(this.signals.has(id))
|
|
{
|
|
for (const callback of this.signals.get(id)) {
|
|
callback(payload);
|
|
}
|
|
}else console.warn("Missing event sended from server");
|
|
}
|
|
}
|
|
WSJS.prototype.events = new EventTarget();
|
|
WSJS.prototype.scope = function(func){
|
|
if(this.isActive)
|
|
{
|
|
func();
|
|
}else this.events.addEventListener("open", func);
|
|
}
|
|
|
|
WSJS.prototype.sendOnly = function(obj){
|
|
if(this.isActive)
|
|
{
|
|
this.sendRaw([obj,'R']);
|
|
}else throw new Error(`socket could be a active`);
|
|
}
|
|
WSJS.prototype.requests = new Map();
|
|
WSJS.prototype.requestCount = 0;
|
|
WSJS.prototype.request = async function(obj){
|
|
if(this.isActive)
|
|
{
|
|
return await new Promise(ok => {
|
|
let id = ++this.requestCount;
|
|
this.sendRaw([obj,id,'R']);
|
|
this.requests.set(id,data => {
|
|
ok(data);
|
|
});
|
|
})
|
|
}else throw new Error(`socket could be a active`);
|
|
}
|
|
WSJS.prototype.stream = async function(obj,callback){
|
|
if(this.isActive)
|
|
{
|
|
let id = ++this.requestCount;
|
|
this.sendRaw([obj, id, 'S']);
|
|
this.requests.set(id,data => {
|
|
callback(data);
|
|
});
|
|
}else throw new Error(`socket could be a active`);
|
|
}
|
|
WSJS.prototype.signals = new Map();
|
|
WSJS.prototype.signal = async function(name, callback){
|
|
if(!this.signals.has(name))
|
|
{
|
|
this.signals.set(name, [callback]);
|
|
}else{
|
|
this.signals.get(name).push(callback);
|
|
}
|
|
}
|
|
WSJS.prototype.slot = async function(name, obj){
|
|
if(this.isActive)
|
|
{
|
|
if(typeof name == "string")
|
|
{
|
|
this.sendOnly([obj,name]);
|
|
}else{
|
|
throw new Error(`name could be a string, gived ${typeof name}`);
|
|
}
|
|
}else throw new Error(`socket could be a active`);
|
|
}
|
|
WSJS.prototype.sendRaw = function(obj){
|
|
if(this.isActive)
|
|
{
|
|
this.ws.send(JSON.stringify(obj))
|
|
};
|
|
}
|
|
WSJS.prototype.checkAuth = async function(username, password){
|
|
let {value:isAuth} = await this.request({
|
|
type: 'auth/check'
|
|
});
|
|
return isAuth;
|
|
};
|
|
WSJS.prototype.authWith = async function(username, password){
|
|
if(!await this.checkAuth())
|
|
{
|
|
let requets = await this.request({
|
|
type: 'auth/login',
|
|
username,
|
|
password
|
|
});
|
|
debugger;
|
|
}
|
|
}; |