Sessions working

This commit is contained in:
2021-05-24 22:50:16 +02:00
parent b8b2ec2ebc
commit 968ede6f58

View File

@ -106,7 +106,7 @@ export class Session {
token; token;
httpBaseUrl; httpBaseUrl;
wsConnection; websocket;
numRequests; numRequests;
constructor(domain) { constructor(domain) {
@ -122,8 +122,8 @@ export class Session {
const protocols = this.server.protocols; const protocols = this.server.protocols;
/* /*
if ("wss" in protocols) { if ("ws" in protocols) {
this.wsConnection = new WebSocket(`wss://${host}:${protocols.wss}/_usimp/websocket`, ["usimp"]); this.websocket = new WebSocket(`ws://${host}:${protocols.ws}/_usimp/websocket`, ["usimp"]);
} }
*/ */
@ -147,8 +147,8 @@ export class Session {
async send(endpoint, data, timeout = 2000, forceHttp = false) { async send(endpoint, data, timeout = 2000, forceHttp = false) {
this.numRequests++; this.numRequests++;
if (!forceHttp && this.wsConnection) { if (!forceHttp && this.websocket) {
this.wsConnection.send(JSON.stringify({ this.websocket.send(JSON.stringify({
'request_num': this.numRequests, 'request_num': this.numRequests,
'data': data 'data': data
})); }));
@ -159,7 +159,6 @@ export class Session {
let headers = { let headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'To-Domain': this.domain.id, 'To-Domain': this.domain.id,
'From-Domain': this.domain.id,
}; };
if (this.token) { if (this.token) {
headers['Authorization'] = `usimp ${this.token}`; headers['Authorization'] = `usimp ${this.token}`;
@ -186,7 +185,7 @@ export class Session {
const resHttp = await this.send("ping", {}, undefined, true); const resHttp = await this.send("ping", {}, undefined, true);
result.http = resHttp.duration; result.http = resHttp.duration;
if (this.wsConnection) { if (this.websocket) {
const resWs = await this.send("ping", {}); const resWs = await this.send("ping", {});
result.ws = resWs.duration; result.ws = resWs.duration;
} }
@ -215,13 +214,12 @@ export class Session {
subscribe(func) { subscribe(func) {
this.numRequests++; this.numRequests++;
if (this.wsConnection) { if (this.websocket) {
// TODO // TODO
} else { } else {
let headers = { let headers = {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'To-Domain': this.domain.id, 'To-Domain': this.domain.id,
'From-Domain': this.domain.id,
}; };
if (this.token) { if (this.token) {
headers['Authorization'] = `usimp ${this.token}`; headers['Authorization'] = `usimp ${this.token}`;
@ -234,8 +232,14 @@ export class Session {
}).then(response => { }).then(response => {
return response.json(); return response.json();
}).then(response => { }).then(response => {
this.subscribe(func); if (response.status === "success") {
func(response); this.subscribe(func);
func(response);
} else {
setTimeout(() => {
this.subscribe(func);
}, 1000);
}
}).catch(() => { }).catch(() => {
setTimeout(() => { setTimeout(() => {
this.subscribe(func); this.subscribe(func);