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