send auth payload on websocket open
close session on logout add socket reconnect on close
This commit is contained in:
parent
87dce82c53
commit
0079c72ea0
13 changed files with 83 additions and 38 deletions
4
.env
4
.env
|
|
@ -1,4 +0,0 @@
|
|||
|
||||
BUSINESS_URL="http://localhost:3000"
|
||||
|
||||
PLATFORM_URL="ws://127.0.0.1:8760"
|
||||
|
|
@ -24,7 +24,6 @@
|
|||
"animejs": "^3.2.0",
|
||||
"axios": "^0.21.1",
|
||||
"core-js": "^3.6.5",
|
||||
"dotenv": "^10.0.0",
|
||||
"electron-is-dev": "^2.0.0",
|
||||
"electron-store": "^8.0.0",
|
||||
"electron-updater": "^4.3.8",
|
||||
|
|
|
|||
12
src/App.vue
12
src/App.vue
|
|
@ -85,8 +85,14 @@ export default defineComponent({
|
|||
|
||||
}
|
||||
|
||||
const onSessionAuthFail = (_event: IpcRendererEvent, _payload: null) => {
|
||||
clearProfile();
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
console.log("APP:mounted.");
|
||||
|
||||
window.ipcRenderer.on("session-auth-fail", onSessionAuthFail)
|
||||
window.ipcRenderer.on("update-state", updateState)
|
||||
window.ipcRenderer.on("update_available", () => {
|
||||
console.log('testing auto update');
|
||||
|
|
@ -99,17 +105,16 @@ export default defineComponent({
|
|||
postMount();
|
||||
|
||||
try {
|
||||
|
||||
const profile = await invokeProfile() as Profile;
|
||||
setProfile(profile);
|
||||
} catch(e) {
|
||||
console.log('[AUTH]', e);
|
||||
console.log('[REND] Auth:', e);
|
||||
clearProfile();
|
||||
} finally {
|
||||
ready.value = true;
|
||||
if (profile.value.crimataId) {
|
||||
// start session
|
||||
postInitSession(profile.value.crimataId);
|
||||
postInitSession();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -117,6 +122,7 @@ export default defineComponent({
|
|||
|
||||
onUnmounted(() => {
|
||||
window.ipcRenderer.removeAllListeners("update-state");
|
||||
window.ipcRenderer.removeAllListeners("session-auth-fail");
|
||||
})
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
import { initApp } from './background/init';
|
||||
import { protocol } from "electron";
|
||||
require('dotenv').config()
|
||||
|
||||
// Scheme must be registered before the app is ready
|
||||
protocol.registerSchemesAsPrivileged([
|
||||
|
|
@ -18,9 +17,9 @@ protocol.registerSchemesAsPrivileged([
|
|||
const isDev = require('electron-is-dev');
|
||||
|
||||
// NOTE Program Begins Here
|
||||
(async () => {
|
||||
(() => {
|
||||
|
||||
console.log('Starting Crimata electron app.');
|
||||
await initApp(isDev);
|
||||
initApp(isDev);
|
||||
|
||||
})();
|
||||
|
|
|
|||
13
src/background/authPayload.ts
Normal file
13
src/background/authPayload.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
|
||||
import { store } from "@/background/store";
|
||||
|
||||
interface PlatformAuthProtocol {
|
||||
key: string;
|
||||
crimata_id: string;
|
||||
}
|
||||
|
||||
export const getAuthPayload = (): PlatformAuthProtocol => ({
|
||||
key: store.get('key'),
|
||||
crimata_id: store.get('crimataId')
|
||||
});
|
||||
|
||||
|
|
@ -5,6 +5,7 @@ import { Profile } from "@/types";
|
|||
import { submit, fetchProfile, logout } from "@/api/account";
|
||||
import { ipcMain, IpcMainInvokeEvent } from "electron";
|
||||
import { store } from "@/background/store";
|
||||
import { endSession } from "@/background/session";
|
||||
|
||||
|
||||
const parseAuthRes = (authRes: any) => {
|
||||
|
|
@ -70,7 +71,7 @@ const onLogin = async (
|
|||
resolve(parsed.profile);
|
||||
|
||||
} catch(e) {
|
||||
console.log('[API]', e.response);
|
||||
console.log('[API]', e);
|
||||
reject(new Error('Failed to authenticate'));
|
||||
}
|
||||
}
|
||||
|
|
@ -94,6 +95,7 @@ const onLogout = async (
|
|||
store.delete('crimataId');
|
||||
|
||||
// TODO: kill crimata platform session
|
||||
endSession();
|
||||
|
||||
resolve();
|
||||
} catch(e) {
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@ import { ClientMessage } from "@/types";
|
|||
// Instantiate socket session with crimata-platorm.
|
||||
const onSessionInit = (
|
||||
_event: IpcMainInvokeEvent,
|
||||
cid: string
|
||||
_payload: null
|
||||
): void => {
|
||||
|
||||
console.log('[IPC]: init-session');
|
||||
|
||||
initSession(cid);
|
||||
initSession();
|
||||
|
||||
initAudioIO();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
import { backgroundMitt } from "@/modules/emitter";
|
||||
import { ipcEmit, loadState } from './helpers';
|
||||
import WebSocket from 'ws';
|
||||
|
||||
import useWebSockets from "./websockets";
|
||||
|
||||
|
|
@ -22,11 +23,17 @@ let win = true;
|
|||
// Info saved to json on quit (key, newMessages).
|
||||
let state: SessionState;
|
||||
|
||||
let socket: WebSocket | null = null;
|
||||
|
||||
|
||||
// Calls appropriate endpoint for a server message.
|
||||
const onMessage = (data: string): void => {
|
||||
let message = JSON.parse(data);
|
||||
console.log('received new message', message);
|
||||
|
||||
if (message === "CLOSE_AUTH_FAIL") {
|
||||
ipcEmit("session-auth-fail", null)
|
||||
return;
|
||||
}
|
||||
|
||||
// Standard message.
|
||||
if (message.content) {
|
||||
|
|
@ -85,16 +92,24 @@ export const sendMessage = (payload: Record<string, any>): void => {
|
|||
|
||||
}
|
||||
|
||||
export const endSession = (): void => {
|
||||
if (socket) {
|
||||
socket.close();
|
||||
socket = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Call this to initialize session with Crimata servers.
|
||||
export const initSession = (cid: string): void => {
|
||||
export const initSession = (): void => {
|
||||
console.log("SESS:Creating new session.")
|
||||
|
||||
// Load Json or createState.
|
||||
state = loadState("session.json");
|
||||
|
||||
// Open socket connection.
|
||||
createSocket();
|
||||
if (!socket)
|
||||
socket = createSocket();
|
||||
|
||||
// Keep win up-to-date.
|
||||
backgroundMitt.on('window-active', (state: boolean) => {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
import WebSocket from 'ws';
|
||||
import { getAuthPayload } from "./authPayload";
|
||||
|
||||
let socket: WebSocket;
|
||||
|
||||
const socketUrl = process.env.PLATFORM_URL;
|
||||
const socketUrl = "ws://127.0.0.1:8760"
|
||||
|
||||
// Run every time we want to connect to backend.
|
||||
export default function useWebSockets(
|
||||
|
|
@ -41,7 +43,8 @@ export default function useWebSockets(
|
|||
const onOpen = (_event: WebSocket.OpenEvent) => {
|
||||
|
||||
console.log("WS:Connected to WS Server!");
|
||||
|
||||
const jwt = getAuthPayload();
|
||||
socket.send(JSON.stringify(jwt));
|
||||
if (openCallback) openCallback();
|
||||
|
||||
}
|
||||
|
|
@ -54,29 +57,30 @@ export default function useWebSockets(
|
|||
|
||||
}
|
||||
|
||||
const onClose = (_event: WebSocket.CloseEvent) => {
|
||||
console.log("WS:Socket closed normally.")
|
||||
const onClose = (event: WebSocket.CloseEvent) => {
|
||||
console.log("WS:Socket closed normally.", event.wasClean)
|
||||
if (!event.wasClean) {
|
||||
setTimeout(createSocket, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
// Reconnect automatically on error.
|
||||
const onError = (event: WebSocket.ErrorEvent) => {
|
||||
console.log("WS:WebSocket error: ", event.message);
|
||||
|
||||
console.log("Attempting reconnect in 1s.")
|
||||
setTimeout(createSocket, 1000)
|
||||
}
|
||||
|
||||
const createSocket = () => {
|
||||
if (socketUrl)
|
||||
socket = new WebSocket(socketUrl)
|
||||
const createSocket = (): WebSocket => {
|
||||
|
||||
socket = new WebSocket(socketUrl);
|
||||
|
||||
// Add listeners.
|
||||
socket.addEventListener("open", onOpen)
|
||||
socket.addEventListener("message", onServerMessage)
|
||||
socket.addEventListener("close", onClose)
|
||||
socket.addEventListener("error", onError)
|
||||
socket.addEventListener("open", onOpen);
|
||||
socket.addEventListener("message", onServerMessage);
|
||||
socket.addEventListener("close", onClose);
|
||||
socket.addEventListener("error", onError);
|
||||
|
||||
return socket;
|
||||
|
||||
console.log("WS:New socket created.")
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -40,13 +40,14 @@ import { authRequest } from '@/modules/message';
|
|||
import { useProfile } from '@/modules/auth';
|
||||
import { invokeLogin } from "@/ipcRend/account";
|
||||
import { Profile } from "@/types";
|
||||
import { postInitSession } from "@/ipcRend/session";
|
||||
|
||||
export default defineComponent({
|
||||
name: "Login",
|
||||
|
||||
setup() {
|
||||
|
||||
const { setProfile } = useProfile();
|
||||
const { profile, setProfile } = useProfile();
|
||||
|
||||
const usr = ref("");
|
||||
const pwd = ref("");
|
||||
|
|
@ -63,6 +64,13 @@ export default defineComponent({
|
|||
setProfile(profile);
|
||||
|
||||
} catch(e) {
|
||||
console.log('Error login in.')
|
||||
} finally{
|
||||
|
||||
if (profile.value.crimataId) {
|
||||
// start session
|
||||
postInitSession();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,11 +11,13 @@ export const postMount = (): void => (
|
|||
);
|
||||
|
||||
|
||||
export const postInitSession = (cid: string): void => (
|
||||
post("init-session", cid)
|
||||
export const postInitSession = (): void => (
|
||||
post("init-session", null)
|
||||
);
|
||||
|
||||
|
||||
export const postMessage = (payload: ClientMessage): void => (
|
||||
post('client-message', payload)
|
||||
);
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import axios, { AxiosRequestConfig } from 'axios';
|
|||
|
||||
const preFix = '/api';
|
||||
|
||||
const baseURL = process.env.BUSSINESS_URL + preFix;
|
||||
const baseURL = "http://127.0.0.1:3000" + preFix;
|
||||
|
||||
|
||||
interface Request {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ const wss = new WebSocket.Server({
|
|||
let auth = false;
|
||||
|
||||
wss.on("connection", function connection(ws, req) {
|
||||
|
||||
ws.on("message", function incoming(message) {
|
||||
|
||||
console.log(message)
|
||||
|
|
@ -19,13 +20,13 @@ wss.on("connection", function connection(ws, req) {
|
|||
auth = true;
|
||||
} else {
|
||||
const parsed = JSON.parse(message);
|
||||
console.log('got a message', message)
|
||||
console.log('got a message', message);
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
const parsed = JSON.parse(message);
|
||||
console.log('parsed', parsed)
|
||||
console.log('parsed', parsed);
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue