aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/lib/socket.js43
-rw-r--r--src/routes/__layout.svelte19
-rw-r--r--src/routes/invoice.js4
-rw-r--r--src/routes/invoice.svelte15
4 files changed, 53 insertions, 28 deletions
diff --git a/src/lib/socket.js b/src/lib/socket.js
index 978c053a..0360acd5 100644
--- a/src/lib/socket.js
+++ b/src/lib/socket.js
@@ -1,4 +1,4 @@
-import { rate } from "$lib/store";
+import { rate, ws } from '$lib/store';
export const messages = (data) => ({
rate() {
rate.set(data);
@@ -8,3 +8,44 @@ export const messages = (data) => ({
console.log(data);
}
});
+
+const token =
+ 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImJvYiIsImlhdCI6MTY1ODc5NDkxMX0._zFTEADrbLffcXa5sIMrJtJ483VdIiYmsdt2ofibxu4';
+const initialReconnectDelay = 1000;
+const maxReconnectDelay = 16000;
+
+let currentReconnectDelay = initialReconnectDelay;
+let socket;
+
+export function connect() {
+ socket = new WebSocket('ws://localhost:3119/ws');
+ socket.addEventListener('open', onWebsocketOpen);
+ socket.addEventListener('close', onWebsocketClose);
+ socket.addEventListener('message', onWebsocketMessage);
+
+ ws.set(socket);
+}
+
+function onWebsocketMessage(msg) {
+ let { type, data } = JSON.parse(msg.data);
+ messages(data)[type] && messages(data)[type]();
+}
+
+function onWebsocketOpen() {
+ currentReconnectDelay = initialReconnectDelay;
+ socket.send(JSON.stringify({ type: 'login', data: token }));
+}
+
+function onWebsocketClose() {
+ ws.set(null);
+ setTimeout(() => {
+ reconnectToWebsocket();
+ }, currentReconnectDelay + Math.floor(Math.random() * 3000));
+}
+
+function reconnectToWebsocket() {
+ if (currentReconnectDelay < maxReconnectDelay) {
+ currentReconnectDelay *= 2;
+ }
+ connect();
+}
diff --git a/src/routes/__layout.svelte b/src/routes/__layout.svelte
index 0168bb7c..f5576edd 100644
--- a/src/routes/__layout.svelte
+++ b/src/routes/__layout.svelte
@@ -1,24 +1,9 @@
<script>
import '../app.css';
import { onMount } from 'svelte';
- import { rate, ws } from "$lib/store";
- import { messages } from "$lib/socket";
+ import { connect } from '$lib/socket';
- let token =
- 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImJvYiIsImlhdCI6MTY1ODc5NDkxMX0._zFTEADrbLffcXa5sIMrJtJ483VdIiYmsdt2ofibxu4';
-
- onMount(async () => {
- $ws = new WebSocket(`ws://localhost:3119/ws`);
-
- $ws.onmessage = (msg) => {
- let { type, data } = JSON.parse(msg.data);
- messages(data)[type] && messages(data)[type]();
- };
-
- $ws.onopen = () => {
- ws.send(JSON.stringify({ type: 'login', data: token }));
- };
- });
+ onMount(connect);
</script>
<slot />
diff --git a/src/routes/invoice.js b/src/routes/invoice.js
index fa493fe1..49bcfe70 100644
--- a/src/routes/invoice.js
+++ b/src/routes/invoice.js
@@ -10,11 +10,11 @@ export async function GET({ params }) {
})
.text();
- let body = await got
+ await got
.post('http://localhost:3119/invoice', {
json: { invoice: { text, network }, user: { username } }
})
.json();
- return { body };
+ return { body: { text } };
}
diff --git a/src/routes/invoice.svelte b/src/routes/invoice.svelte
index 6ea9e67f..40a66a24 100644
--- a/src/routes/invoice.svelte
+++ b/src/routes/invoice.svelte
@@ -1,13 +1,12 @@
<script>
- import { rate } from "$lib/store";
- import { onMount } from "svelte";
+ import { rate } from '$lib/store';
+ import { onMount } from 'svelte';
- onMount(() => {
+ export let text;
+</script>
- });
- </script>
+QR code
- QR code
-
- {$rate}
+{text}
+{$rate}