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 { ipcMain, IpcMainInvokeEvent } from "electron";
import { store } from "@/background/store";
import { endSession } from "@/background/session";
const parseAuthRes = (authRes: any) => {
@ -94,6 +95,7 @@ const onLogout = async (
store.delete('crimataId');
// TODO: kill crimata platform session
endSession();
resolve();
} catch(e) {

View file

@ -10,6 +10,7 @@
import { backgroundMitt } from "@/modules/emitter";
import { ipcEmit, loadState } from './helpers';
import WebSocket from 'ws';
import useWebSockets from "./websockets";
@ -22,6 +23,8 @@ 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 => {
@ -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.
export const initSession = (authPayload: {
@ -100,7 +110,8 @@ export const initSession = (authPayload: {
state = loadState("session.json");
// Open socket connection.
createSocket(authPayload);
if (!socket)
socket = createSocket(authPayload);
// Keep win up-to-date.
backgroundMitt.on('window-active', (state: boolean) => {

View file

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

View file

@ -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);
}
});