From ac8f338d758cd19dd95648f3ed62a3d91714ac06 Mon Sep 17 00:00:00 2001 From: Enrique Hernandez Date: Thu, 3 Jun 2021 09:55:50 -0400 Subject: [PATCH] close session on logout --- src/background/ipc/account.ts | 2 ++ src/background/session.ts | 13 ++++++++++++- src/background/websockets.ts | 6 ++++-- tests/server.js | 5 +++-- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/background/ipc/account.ts b/src/background/ipc/account.ts index c9594be..5898509 100644 --- a/src/background/ipc/account.ts +++ b/src/background/ipc/account.ts @@ -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) { diff --git a/src/background/session.ts b/src/background/session.ts index fac6572..ebfbc26 100644 --- a/src/background/session.ts +++ b/src/background/session.ts @@ -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): 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) => { diff --git a/src/background/websockets.ts b/src/background/websockets.ts index 5556ece..36eae6f 100644 --- a/src/background/websockets.ts +++ b/src/background/websockets.ts @@ -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 { diff --git a/tests/server.js b/tests/server.js index 6731be9..9616e8a 100644 --- a/tests/server.js +++ b/tests/server.js @@ -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); } }); -- 2.43.0