Subscriptions working
This commit is contained in:
@ -100,7 +100,8 @@ export class App {
|
|||||||
this.defaultLocation = "/";
|
this.defaultLocation = "/";
|
||||||
this.setHash("/");
|
this.setHash("/");
|
||||||
} else {
|
} else {
|
||||||
throw Error(response.message);
|
console.error(response.error);
|
||||||
|
throw Error(response.error.message || response.error.code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -237,14 +238,14 @@ export class App {
|
|||||||
this.addMessage(input.value);
|
this.addMessage(input.value);
|
||||||
const val = input.value;
|
const val = input.value;
|
||||||
input.value = "";
|
input.value = "";
|
||||||
await this.session.sendEvent("60nc0XXDIYUh6QzX4p0rMpCdzDmxghZLZk8dLuQh628", {
|
await this.session.newEvent("60nc0XXDIYUh6QzX4p0rMpCdzDmxghZLZk8dLuQh628", {
|
||||||
message: val,
|
message: val,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.session.subscribe(response => {
|
this.session.subscribe(response => {
|
||||||
this.addMessage(response.data.event.data.message);
|
this.addMessage(response.data.data.message);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
87
src/usimp.ts
87
src/usimp.ts
@ -19,12 +19,15 @@ interface DomainServerJson {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EventJson {
|
interface OutputEnvelopeJson {
|
||||||
data: {
|
status: string,
|
||||||
event: {
|
request_nr: number,
|
||||||
data: any
|
data: any,
|
||||||
}
|
error: {
|
||||||
}
|
code: string,
|
||||||
|
message: string | null,
|
||||||
|
description: string | null,
|
||||||
|
} | null,
|
||||||
}
|
}
|
||||||
|
|
||||||
interface WellKnownJson {
|
interface WellKnownJson {
|
||||||
@ -145,6 +148,7 @@ export class Session {
|
|||||||
httpBaseUrl: string | null;
|
httpBaseUrl: string | null;
|
||||||
websocket: WebSocket | null;
|
websocket: WebSocket | null;
|
||||||
numRequests: number;
|
numRequests: number;
|
||||||
|
subscriptions: string[];
|
||||||
|
|
||||||
constructor(domain: Domain) {
|
constructor(domain: Domain) {
|
||||||
this.domain = domain;
|
this.domain = domain;
|
||||||
@ -153,6 +157,7 @@ export class Session {
|
|||||||
this.token = null;
|
this.token = null;
|
||||||
this.httpBaseUrl = null;
|
this.httpBaseUrl = null;
|
||||||
this.websocket = null;
|
this.websocket = null;
|
||||||
|
this.subscriptions = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
async chooseDomainServer(): Promise<{ http: number | null, ws: number | null } | undefined> {
|
async chooseDomainServer(): Promise<{ http: number | null, ws: number | null } | undefined> {
|
||||||
@ -238,28 +243,43 @@ export class Session {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async send(endpoint: string, data: object, timeout: number = 2000, forceHttp: boolean = false) {
|
async send(endpoint: string, data: any, timeout: number = 2000, forceHttp: boolean = false, cb: ((a: OutputEnvelopeJson) => void) | null = null) {
|
||||||
this.numRequests++;
|
this.numRequests++;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
if (!forceHttp && this.websocket) {
|
if (!forceHttp && this.websocket !== null) {
|
||||||
const req_nr = this.numRequests;
|
const req_nr = this.numRequests;
|
||||||
const response = this.waitForWebSocketResponse(req_nr, timeout);
|
|
||||||
|
|
||||||
const startTime = performance.now();
|
const startTime = performance.now();
|
||||||
|
|
||||||
|
let response;
|
||||||
|
if (cb === null) {
|
||||||
|
response = this.waitForWebSocketResponse(req_nr, timeout);
|
||||||
|
} else {
|
||||||
|
this.websocket.addEventListener("message", (msg) => {
|
||||||
|
const data = JSON.parse(msg.data);
|
||||||
|
if (data['request_nr'] === req_nr) {
|
||||||
|
cb(data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
await this.waitForWebSocket();
|
await this.waitForWebSocket();
|
||||||
await this.websocket.send(JSON.stringify({
|
await this.websocket.send(JSON.stringify({
|
||||||
'request_nr': req_nr,
|
'request_nr': req_nr,
|
||||||
'endpoint': endpoint,
|
'endpoint': endpoint,
|
||||||
'to_domain': this.domain.id,
|
'to_domain': this.domain.id,
|
||||||
|
'token': this.token,
|
||||||
'data': data
|
'data': data
|
||||||
}));
|
}));
|
||||||
const responseData = await response;
|
|
||||||
|
|
||||||
|
if (cb === null) {
|
||||||
|
const responseData = await response;
|
||||||
const endTime = performance.now();
|
const endTime = performance.now();
|
||||||
responseData.duration = endTime - startTime;
|
responseData.duration = endTime - startTime;
|
||||||
return responseData;
|
return responseData;
|
||||||
|
} else {
|
||||||
|
return null; // TODO subscription id
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const timer = setTimeout(() => controller.abort(), timeout);
|
const timer = setTimeout(() => controller.abort(), timeout);
|
||||||
@ -322,46 +342,37 @@ export class Session {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendEvent(roomId: string, data: object) {
|
async newEvent(roomId: string, data: any) {
|
||||||
return this.send("send_event", {
|
return this.send("new_event", {
|
||||||
room_id: roomId,
|
room_id: roomId,
|
||||||
data: data,
|
events: [{
|
||||||
|
data: data
|
||||||
|
}],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
subscribe(func: (a: EventJson) => void) {
|
async subscribe(cb: (a: OutputEnvelopeJson) => void) {
|
||||||
this.numRequests++;
|
this.numRequests++;
|
||||||
if (this.websocket) {
|
if (this.websocket !== null) {
|
||||||
// TODO
|
let subscription = await this.send('subscribe', {}, 60_000, false, cb);
|
||||||
|
this.subscriptions.push(subscription);
|
||||||
|
return subscription;
|
||||||
} else {
|
} else {
|
||||||
let headers: Record<string, string> = {
|
this.send('subscribe', {}, 60_000).then((res) => {
|
||||||
'Content-Type': 'application/json',
|
if (res.status === "success") {
|
||||||
'To-Domain': this.domain.id,
|
this.subscribe(cb);
|
||||||
};
|
cb(res);
|
||||||
if (this.token) {
|
|
||||||
headers['Authorization'] = `usimp ${this.token}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch(`${this.httpBaseUrl}/subscribe`, {
|
|
||||||
method: "POST",
|
|
||||||
headers: headers,
|
|
||||||
body: JSON.stringify({}),
|
|
||||||
}).then(response => {
|
|
||||||
return response.json();
|
|
||||||
}).then(response => {
|
|
||||||
if (response.status === "success") {
|
|
||||||
this.subscribe(func);
|
|
||||||
func(response);
|
|
||||||
} else {
|
} else {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.subscribe(func);
|
this.subscribe(cb);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
}
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.subscribe(func);
|
this.subscribe(cb);
|
||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
return null; // TODO subscription id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user