blob: c7c541e17f4aa56a5f8f7cde830e084a083246b7 (
plain) (
blame)
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888 } /* Comment */
.highlight .err { color: #A61717; background-color: #E3D2D2 } /* Error */
.highlight .k { color: #080; font-weight: bold } /* Keyword */
.highlight .ch { color: #888 } /* Comment.Hashbang */
.highlight .cm { color: #888 } /* Comment.Multiline */
.highlight .cp { color: #C00; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888 } /* Comment.Single */
.highlight .cs { color: #C00; font-weight: bold; background-color: #FFF0F0 } /* Comment.Special */
.highlight .gd { color: #000; background-color: #FDD } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #A00 } /* Generic.Error */
.highlight .gh { color: #333 } /* Generic.Heading */
.highlight .gi { color: #000; background-color: #DFD } /* Generic.Inserted */
.highlight .go { color: #888 } /* Generic.Output */
.highlight .gp { color: #555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666 } /* Generic.Subheading */
.highlight .gt { color: #A00 } /* Generic.Traceback */
.highlight .kc { color: #080; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #080; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #080; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #080 } /* Keyword.Pseudo */
.highlight .kr { color: #080; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #00D; font-weight: bold } /* Literal.Number */
.highlight .s { color: #D20; background-color: #FFF0F0 } /* Literal.String */
.highlight .na { cimport { get } from 'svelte/store';
import { rate, user, token } from '$lib/store';
let interval, socket;
export const auth = () => {
let t = get(token);
if (t) send('login', t);
};
export const send = (type, data) => {
socket?.readyState === 1 && socket.send(JSON.stringify({ type, data }));
};
export const messages = (data) => ({
rate() {
rate.set(data);
},
payment() {
console.log(data, 'payment');
},
connected: auth,
login() {
user.set(data);
}
});
const initialReconnectDelay = 1000;
const maxReconnectDelay = 16000;
let currentReconnectDelay = initialReconnectDelay;
export function connect() {
clearInterval(interval);
if (socket) return auth();
socket = new WebSocket('ws://localhost:3119/ws');
socket.addEventListener('open', onWebsocketOpen);
socket.addEventListener('close', onWebsocketClose);
socket.addEventListener('message', onWebsocketMessage);
interval = setInterval(() => send('heartbeat'), 5000);
}
export function close() {
if (socket) socket.close();
}
function onWebsocketMessage(msg) {
let { type, data } = JSON.parse(msg.data);
messages(data)[type] && messages(data)[type]();
}
function onWebsocketOpen() {
currentReconnectDelay = initialReconnectDelay;
}
function onWebsocketClose() {
socket = null;
setTimeout(() => {
reconnectToWebsocket();
}, currentReconnectDelay + Math.floor(Math.random() * 3000));
}
function reconnectToWebsocket() {
if (currentReconnectDelay < maxReconnectDelay) {
currentReconnectDelay *= 2;
}
connect();
}
|