frontend: gateway reconnect + add frontend directory to express server
This commit is contained in:
parent
a9a4cdbb5c
commit
d21408ac63
4 changed files with 29 additions and 10 deletions
|
@ -21,16 +21,28 @@ export default {
|
||||||
heartbeatInterval: null,
|
heartbeatInterval: null,
|
||||||
user: null,
|
user: null,
|
||||||
channels: null,
|
channels: null,
|
||||||
|
reconnectDelay: 400,
|
||||||
|
reconnectTimeout: null,
|
||||||
init() {
|
init() {
|
||||||
|
const token = getAuthToken();
|
||||||
|
if (!token) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
this.ws = new WebSocket(getItem("gatewayBase"));
|
this.ws = new WebSocket(getItem("gatewayBase"));
|
||||||
this.ws.onmessage = (message) => {
|
this.ws.onopen = () => {
|
||||||
const payload = JSON.parse(message);
|
if (this.reconnectTimeout) {
|
||||||
|
clearTimeout(this.reconnectTimeout);
|
||||||
|
}
|
||||||
|
this.reconnectDelay = 400;
|
||||||
|
};
|
||||||
|
this.ws.onmessage = (event) => {
|
||||||
|
const payload = JSON.parse(event.data);
|
||||||
|
|
||||||
switch (payload.t) {
|
switch (payload.t) {
|
||||||
case GatewayPayloadType.Hello: {
|
case GatewayPayloadType.Hello: {
|
||||||
this.send({
|
this.send({
|
||||||
t: GatewayPayloadType.Authenticate,
|
t: GatewayPayloadType.Authenticate,
|
||||||
d: getAuthToken()
|
d: token
|
||||||
});
|
});
|
||||||
|
|
||||||
this.heartbeatInterval = setInterval(() => {
|
this.heartbeatInterval = setInterval(() => {
|
||||||
|
@ -49,13 +61,19 @@ export default {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
this.ws.onclose = () => {
|
this.ws.onclose = () => {
|
||||||
|
this.reconnectDelay *= 2;
|
||||||
this.authenticated = false;
|
this.authenticated = false;
|
||||||
this.user = null;
|
this.user = null;
|
||||||
this.channels = null;
|
this.channels = null;
|
||||||
if (this.heartbeatInterval) {
|
if (this.heartbeatInterval) {
|
||||||
clearInterval(this.heartbeatInterval);
|
clearInterval(this.heartbeatInterval);
|
||||||
}
|
}
|
||||||
|
this.reconnectTimeout = setTimeout(() => {
|
||||||
|
this.init();
|
||||||
|
}, this.reconnectDelay);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return true;
|
||||||
},
|
},
|
||||||
send(data) {
|
send(data) {
|
||||||
return this.ws.send(JSON.stringify(data));
|
return this.ws.send(JSON.stringify(data));
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
import App from './components/App.svelte';
|
import App from './components/App.svelte';
|
||||||
|
import gateway from './gateway';
|
||||||
|
import { initStorageDefaults } from './storage';
|
||||||
|
|
||||||
|
initStorageDefaults();
|
||||||
|
gateway.init();
|
||||||
|
|
||||||
const app = new App({
|
const app = new App({
|
||||||
target: document.body
|
target: document.body
|
||||||
|
|
|
@ -14,10 +14,7 @@ const dummyProvider = {
|
||||||
};
|
};
|
||||||
|
|
||||||
function getProvider() {
|
function getProvider() {
|
||||||
if (!window.localStorage || !window.localStorage.getItem || !window.localStorage.setItem) {
|
return window.localStorage || dummyProvider;
|
||||||
return dummyProvider;
|
|
||||||
}
|
|
||||||
return window.localStorage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getItem(key) {
|
export function getItem(key) {
|
||||||
|
|
|
@ -1,13 +1,12 @@
|
||||||
import { Application, json } from "express";
|
import express, { Application, json } from "express";
|
||||||
import usersRouter from "./routes/api/v1/users";
|
import usersRouter from "./routes/api/v1/users";
|
||||||
import channelsRouter from "./routes/api/v1/channels";
|
import channelsRouter from "./routes/api/v1/channels";
|
||||||
import messagesRouter from "./routes/api/v1/messages";
|
import messagesRouter from "./routes/api/v1/messages";
|
||||||
|
|
||||||
export default function(app: Application) {
|
export default function(app: Application) {
|
||||||
app.get("/", (req, res) => res.send("hello!"));
|
|
||||||
|
|
||||||
app.use(json());
|
app.use(json());
|
||||||
app.use("/api/v1/users", usersRouter);
|
app.use("/api/v1/users", usersRouter);
|
||||||
app.use("/api/v1/channels", channelsRouter);
|
app.use("/api/v1/channels", channelsRouter);
|
||||||
app.use("/api/v1/messages", messagesRouter);
|
app.use("/api/v1/messages", messagesRouter);
|
||||||
|
app.use("/", express.static("frontend/public"));
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue