2022-02-26 04:25:04 +00:00
|
|
|
import {basicAuth, encodeBase64Url, topicShortUrl, topicUrlWs} from "./utils";
|
2022-02-24 14:52:49 +00:00
|
|
|
|
2022-03-04 16:08:32 +00:00
|
|
|
const retryBackoffSeconds = [5, 10, 15, 20, 30];
|
2022-02-24 14:52:49 +00:00
|
|
|
|
2022-03-11 20:17:12 +00:00
|
|
|
/**
|
|
|
|
* A connection contains a single WebSocket connection for one topic. It handles its connection
|
|
|
|
* status itself, including reconnect attempts and backoff.
|
|
|
|
*
|
|
|
|
* Incoming messages and state changes are forwarded via listeners.
|
|
|
|
*/
|
2022-02-24 01:30:12 +00:00
|
|
|
class Connection {
|
2022-03-04 16:08:32 +00:00
|
|
|
constructor(connectionId, subscriptionId, baseUrl, topic, user, since, onNotification, onStateChanged) {
|
2022-03-04 01:07:35 +00:00
|
|
|
this.connectionId = connectionId;
|
2022-02-24 01:30:12 +00:00
|
|
|
this.subscriptionId = subscriptionId;
|
2022-02-24 14:52:49 +00:00
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
this.topic = topic;
|
2022-02-26 04:25:04 +00:00
|
|
|
this.user = user;
|
2022-02-24 14:52:49 +00:00
|
|
|
this.since = since;
|
2022-02-26 04:25:04 +00:00
|
|
|
this.shortUrl = topicShortUrl(baseUrl, topic);
|
2022-02-24 01:30:12 +00:00
|
|
|
this.onNotification = onNotification;
|
2022-03-04 16:08:32 +00:00
|
|
|
this.onStateChanged = onStateChanged;
|
2022-02-24 01:30:12 +00:00
|
|
|
this.ws = null;
|
2022-02-24 14:52:49 +00:00
|
|
|
this.retryCount = 0;
|
|
|
|
this.retryTimeout = null;
|
2022-02-24 01:30:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start() {
|
2022-02-24 15:30:58 +00:00
|
|
|
// Don't fetch old messages; we do that as a poll() when adding a subscription;
|
|
|
|
// we don't want to re-trigger the main view re-render potentially hundreds of times.
|
2022-02-26 04:25:04 +00:00
|
|
|
|
|
|
|
const wsUrl = this.wsUrl();
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Opening connection to ${wsUrl}`);
|
2022-02-26 04:25:04 +00:00
|
|
|
|
2022-02-24 14:52:49 +00:00
|
|
|
this.ws = new WebSocket(wsUrl);
|
|
|
|
this.ws.onopen = (event) => {
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Connection established`, event);
|
2022-02-24 14:52:49 +00:00
|
|
|
this.retryCount = 0;
|
2022-03-04 16:08:32 +00:00
|
|
|
this.onStateChanged(this.subscriptionId, ConnectionState.Connected);
|
2022-02-24 01:30:12 +00:00
|
|
|
}
|
2022-02-24 14:52:49 +00:00
|
|
|
this.ws.onmessage = (event) => {
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Message received from server: ${event.data}`);
|
2022-02-24 01:30:12 +00:00
|
|
|
try {
|
|
|
|
const data = JSON.parse(event.data);
|
2022-02-26 15:14:43 +00:00
|
|
|
if (data.event === 'open') {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-24 01:30:12 +00:00
|
|
|
const relevantAndValid =
|
|
|
|
data.event === 'message' &&
|
|
|
|
'id' in data &&
|
|
|
|
'time' in data &&
|
|
|
|
'message' in data;
|
|
|
|
if (!relevantAndValid) {
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Unexpected message. Ignoring.`);
|
2022-02-24 01:30:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-02-28 00:29:17 +00:00
|
|
|
this.since = data.id;
|
2022-02-24 01:30:12 +00:00
|
|
|
this.onNotification(this.subscriptionId, data);
|
|
|
|
} catch (e) {
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Error handling message: ${e}`);
|
2022-02-24 01:30:12 +00:00
|
|
|
}
|
|
|
|
};
|
2022-02-24 14:52:49 +00:00
|
|
|
this.ws.onclose = (event) => {
|
2022-02-24 01:30:12 +00:00
|
|
|
if (event.wasClean) {
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
|
2022-02-24 14:52:49 +00:00
|
|
|
this.ws = null;
|
2022-02-24 01:30:12 +00:00
|
|
|
} else {
|
2022-02-24 14:52:49 +00:00
|
|
|
const retrySeconds = retryBackoffSeconds[Math.min(this.retryCount, retryBackoffSeconds.length-1)];
|
|
|
|
this.retryCount++;
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Connection died, retrying in ${retrySeconds} seconds`);
|
2022-02-24 14:52:49 +00:00
|
|
|
this.retryTimeout = setTimeout(() => this.start(), retrySeconds * 1000);
|
2022-03-04 16:08:32 +00:00
|
|
|
this.onStateChanged(this.subscriptionId, ConnectionState.Connecting);
|
2022-02-24 01:30:12 +00:00
|
|
|
}
|
|
|
|
};
|
2022-02-24 14:52:49 +00:00
|
|
|
this.ws.onerror = (event) => {
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Error occurred: ${event}`, event);
|
2022-02-24 01:30:12 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-02-24 14:52:49 +00:00
|
|
|
close() {
|
2022-03-04 01:07:35 +00:00
|
|
|
console.log(`[Connection, ${this.shortUrl}, ${this.connectionId}] Closing connection`);
|
2022-02-24 14:52:49 +00:00
|
|
|
const socket = this.ws;
|
|
|
|
const retryTimeout = this.retryTimeout;
|
|
|
|
if (socket !== null) {
|
|
|
|
socket.close();
|
2022-02-24 01:30:12 +00:00
|
|
|
}
|
2022-02-24 14:52:49 +00:00
|
|
|
if (retryTimeout !== null) {
|
|
|
|
clearTimeout(retryTimeout);
|
|
|
|
}
|
|
|
|
this.retryTimeout = null;
|
|
|
|
this.ws = null;
|
2022-02-24 01:30:12 +00:00
|
|
|
}
|
2022-02-26 04:25:04 +00:00
|
|
|
|
|
|
|
wsUrl() {
|
|
|
|
const params = [];
|
2022-02-28 00:29:17 +00:00
|
|
|
if (this.since) {
|
|
|
|
params.push(`since=${this.since}`);
|
2022-02-26 04:25:04 +00:00
|
|
|
}
|
2022-03-02 02:23:12 +00:00
|
|
|
if (this.user) {
|
2022-02-26 04:25:04 +00:00
|
|
|
const auth = encodeBase64Url(basicAuth(this.user.username, this.user.password));
|
|
|
|
params.push(`auth=${auth}`);
|
|
|
|
}
|
|
|
|
const wsUrl = topicUrlWs(this.baseUrl, this.topic);
|
|
|
|
return (params.length === 0) ? wsUrl : `${wsUrl}?${params.join('&')}`;
|
|
|
|
}
|
2022-02-24 01:30:12 +00:00
|
|
|
}
|
|
|
|
|
2022-03-04 16:08:32 +00:00
|
|
|
export class ConnectionState {
|
|
|
|
static Connected = "connected";
|
|
|
|
static Connecting = "connecting";
|
|
|
|
}
|
|
|
|
|
2022-02-24 01:30:12 +00:00
|
|
|
export default Connection;
|