close session on logout

This commit is contained in:
Enrique Hernandez 2021-06-03 09:55:50 -04:00
commit ac8f338d75
4 changed files with 21 additions and 5 deletions

View file

@ -5,6 +5,7 @@ import { Profile } from "@/types";
import { submit, fetchProfile, logout } from "@/api/account"; import { submit, fetchProfile, logout } from "@/api/account";
import { ipcMain, IpcMainInvokeEvent } from "electron"; import { ipcMain, IpcMainInvokeEvent } from "electron";
import { store } from "@/background/store"; import { store } from "@/background/store";
import { endSession } from "@/background/session";
const parseAuthRes = (authRes: any) => { const parseAuthRes = (authRes: any) => {
@ -94,6 +95,7 @@ const onLogout = async (
store.delete('crimataId'); store.delete('crimataId');
// TODO: kill crimata platform session // TODO: kill crimata platform session
endSession();
resolve(); resolve();
} catch(e) { } catch(e) {

View file

@ -10,6 +10,7 @@
import { backgroundMitt } from "@/modules/emitter"; import { backgroundMitt } from "@/modules/emitter";
import { ipcEmit, loadState } from './helpers'; import { ipcEmit, loadState } from './helpers';
import WebSocket from 'ws';
import useWebSockets from "./websockets"; import useWebSockets from "./websockets";
@ -22,6 +23,8 @@ let win = true;
// Info saved to json on quit (key, newMessages). // Info saved to json on quit (key, newMessages).
let state: SessionState; let state: SessionState;
let socket: WebSocket | null = null;
// Calls appropriate endpoint for a server message. // Calls appropriate endpoint for a server message.
const onMessage = (data: string): void => { const onMessage = (data: string): void => {
@ -88,6 +91,13 @@ 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. // Call this to initialize session with Crimata servers.
export const initSession = (authPayload: { export const initSession = (authPayload: {
@ -100,7 +110,8 @@ export const initSession = (authPayload: {
state = loadState("session.json"); state = loadState("session.json");
// Open socket connection. // Open socket connection.
createSocket(authPayload); if (!socket)
socket = createSocket(authPayload);
// Keep win up-to-date. // Keep win up-to-date.
backgroundMitt.on('window-active', (state: boolean) => { backgroundMitt.on('window-active', (state: boolean) => {

View file

@ -1,3 +1,4 @@
"use strict"; "use strict";
import WebSocket from 'ws'; import WebSocket from 'ws';
@ -68,7 +69,7 @@ export default function useWebSockets(
const createSocket = (authPayload: { const createSocket = (authPayload: {
token: string; token: string;
crimataId: string; crimataId: string;
}) => { }):WebSocket => {
socket = new WebSocket(socketUrl) socket = new WebSocket(socketUrl)
// Add listeners. // Add listeners.
@ -82,7 +83,8 @@ export default function useWebSockets(
socket.addEventListener("close", onClose) socket.addEventListener("close", onClose)
socket.addEventListener("error", onError) socket.addEventListener("error", onError)
console.log("WS:New socket created.") return socket;
} }
return { return {

View file

@ -6,6 +6,7 @@ const wss = new WebSocket.Server({
let auth = false; let auth = false;
wss.on("connection", function connection(ws, req) { wss.on("connection", function connection(ws, req) {
ws.on("message", function incoming(message) { ws.on("message", function incoming(message) {
console.log(message) console.log(message)
@ -19,13 +20,13 @@ wss.on("connection", function connection(ws, req) {
auth = true; auth = true;
} else { } else {
const parsed = JSON.parse(message); const parsed = JSON.parse(message);
console.log('got a message', message) console.log('got a message', message);
} }
} else { } else {
const parsed = JSON.parse(message); const parsed = JSON.parse(message);
console.log('parsed', parsed) console.log('parsed', parsed);
} }
}); });